0% found this document useful (0 votes)
38 views

Input-Output Technique

The document provides 8 examples of input-output problems and their C and C++ solutions. Each example includes the input specification, output specification, sample input, sample output, and code solutions in C and C++. The examples cover topics such as reading multiple test cases, handling different input formats, calculating sums, products, remainders, maximum/minimum values from input data.

Uploaded by

Rafid Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Input-Output Technique

The document provides 8 examples of input-output problems and their C and C++ solutions. Each example includes the input specification, output specification, sample input, sample output, and code solutions in C and C++. The examples cover topics such as reading multiple test cases, handling different input formats, calculating sums, products, remainders, maximum/minimum values from input data.

Uploaded by

Rafid Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Important input-output techniques

for solving online judge problems

SAMPLE INPUT-OUTPUT - 1

Input Specification:

Input starts with an integer T , denoting the number of test


cases. Each test case will contain two integers a and b.

Output Specification:

For each test case, output one line in the format “Case x: c”
(quotes for clarity), where x is the case number and c is the
sum of a and b .

Sample Input:

2
5 10
94

Sample Output:

Case 1: 15
Case 2: 13
C Code :

#include <stdio.h>
int main(){
int cas,i,a,b,c;
scanf("%d",&cas);
for(i=1;i<=cas;i++){
scanf("%d %d",&a,&b);
c=a+b;
printf("Case %d: %d\n",i,c);
}
return 0;
}

C++ Code:

#include <iostream>
using namespace std;
int main(){
int cas,i,a,b,c;
cin>>cas;
for(i=1;i<=cas;i++){
cin>>a>>b;
c=a+b;
cout<<"Case "<<i<<": "<<c<<endl;
}
return 0;
}
SAMPLE INPUT-OUTPUT - 2

Input Specification:

Input file will consist of several lines. Each line contains two
integers a and b. Input is terminated by a line containing two
zeroes. This line should not be processed.

Output Specification:

For each line of input produce one line of output. This line
contain the value of c, where c = a*b. After each test case, you
should print a blank line.

Sample Input:

22
33
00

Sample Output:

9
C Code:

#include <stdio.h>
int main(){
int a,b,c;

while(scanf("%d %d",&a,&b)==2){
if(a==0&&b==0)break;
c = a*b;
printf("%d\n\n",c);
}
return 0;
}

C++ Code:

#include <iostream>
using namespace std;
int main(){
int a,b,c;

while(cin>>a>>b){
if(a==0&&b==0)break;
c = a*b;
cout<<c<<endl<<endl;
}
return 0;
}
SAMPLE INPUT-OUTPUT - 3

Input Specification:

Input file consist of several test cases. Each test case contains
a line and each line will contains two integers a and b(b>0).
Input is terminated by EOF.

Output Specification:

For each test case you should output a line. This line contains
the value of c wherec=a%b. You should print a blank line
between two consecutive test cases.

Sample Input:

32
23
11

Sample Output:

0
C Code :

#include <stdio.h>
int main(){
int a,b,c,temp=0;
while(scanf("%d %d",&a,&b)==2){
c = a%b;
if(temp)
printf("\n");
printf("%d\n",c);
temp=1;
}
return 0;
}

C++ Code :

#include <iostream>
using namespace std;
int main(){
int a,b,c,temp=0;
while(cin>>a>>b){
c=a%b;
if(temp)
cout<<endl;
cout<<c<<endl;
temp=1;
}
return 0;
}
SAMPLE INPUT-OUTPUT - 4

Input Specification:

The input data for any test case consists of a sequence of one
or more non-negative integers. The last number in each
sequence is -1, which signifies the end of data for that
particular test case. The end of data for the entire input is the
number -1 as the first value in a test case and it is not
considered to be a separate test.

Output Specification:

For each test case produces a line of output, this line contains
the maximum value in a test case.

Sample Input:

4
10
9
-1
5
4
-1
-1

Sample Output:

10
5
.
C Code:

#include <stdio.h>
int main(){
int n,max_val;
while(scanf("%d",&n)==1 && n!=-1){
max_val=n;
while(scanf("%d",&n)==1 && n!=-1)
if(n>max_val) max_val=n;
printf("%d\n",max_val);
}
return 0;
}

C++ Code:

#include <iostream>
using namespace std;
int main(){
int n,max_val;
while(cin>>n && n!=-1){
max_val=n;
while(cin>>n && n!=-1)
if(n>max_val) max_val=n;
cout<<max_val<<endl;
}
return 0;
}
SAMPLE INPUT-OUTPUT - 5

Input Specification:

Input file consists of several test cases. First line of each test
case contains an integer n. Followed by a line contains n
integers which are the age of n students.

Output Specification:

For each test case print a line contains two integers


Maximum and Minimum Age among them.The Two age are
separated by a single space.

Sample Input:

5
10 12 8 13 20
3
789

Sample Output:

8 20
79
C Code:

#include <stdio.h>
int main(){
int n,age,max_age,min_age;
while(scanf("%d",&n)==1){
scanf("%d",&age);
min_age=max_age=age;
n--;
while(n--){
scanf("%d",&age);
if(age<min_age)
min_age=age;
if(age>max_age)
max_age=age;
}
printf("%d %d\n",min_age,max_age);
}
return 0;
}
C++ Code:

#include <iostream>
using namespace std;
int main(){
int n,age,max_age,min_age;
while(cin>>n){
cin>>age;
min_age=max_age=age;
n--;
while(n--){
cin>>age;
if(age<min_age)
min_age=age;
if(age>max_age)
max_age=age;
}
cout<<min_age<<" "<<max_age<<endl;
}
return 0;
}
SAMPLE INPUT-OUTPUT - 6

Input Specification:

The input begins with a single positive integer n on a line by


itself indicating the number of the cases. Each test case
contains a line.This line consists by one or more words.

Output Specification:

For each test case output a single integer in a line which


denotes the length of input string.

Sample Input:

2
Uva Online Judge
www.outsbook.com

Sample Output:

16
16
C Code:

#include <stdio.h>#include <string.h>#define size 1000


int main(){
int length,cas;
char inputStr[size];
scanf("%d",&cas);
getchar(); // For ignoring new line
while(cas--){
gets(inputStr);
length = strlen(inputStr);
printf("%d\n",length);
}
return 0;
}
C++ Code:

#include <iostream>#include <cstdio>#include <string>


using namespace std;
int main(){
int length,cas;
string inputStr;
cin>>cas;
getchar(); // For ignoring new line
while(cas--){
getline(cin,inputStr);
length = inputStr.length();
cout<<length<<endl;
}
return 0;
}
SAMPLE INPUT-OUTPUT - 7

Input Specification:

Input file contains a series of lines. Each line contains one or


more integers.

Output Specification:

For each line of input generate a line of output. This line


contains an integer, which is the minimum number of input
line.

Sample Input:

10 5 7 9 10 3
478

Sample Output:

5
4
C Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 1000

int main(){
char inputStr[size],*tempStr;
int minNum,num;
while(gets(inputStr)){
tempStr = strtok(inputStr," ");
minNum = atoi(tempStr);
while(tempStr!=NULL){
num = atoi(tempStr);
if(num<minNum)
minNum=num;
tempStr=strtok(NULL," ");
}
printf("%d\n",minNum);
}
return 0;
}
C++ Code:

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>

using namespace std;


int main(){
string inputStr,tempStr;
int minNum,num;
while(getline(cin,inputStr)){
istringstream token(inputStr);
token>>tempStr;
minNum = atoi(tempStr.c_str());
while(token){
token >> tempStr;
num = atoi(tempStr.c_str());
if(num<minNum)
minNum=num;
}
cout<<minNum<<endl;
}
return 0;
}
SAMPLE INPUT-OUTPUT - 8

Input Specification:

The first line of Input file contains an integer n denotes the


number of test case.Each test case contains a line. There are
one or more integers in this line.

Output Specification:

For each test case output the maximum number.(See the


sample I/O)

Sample Input:

2
10 5 7 9 10 3
478

Sample Output:

10
8
C Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 1000

int main(){
char inputStr[size],*tempStr;
int minNum,num,cas;
scanf("%d",&cas);
getchar(); // To ignore new line
while(cas--){
gets(inputStr);
tempStr = strtok(inputStr," ");
minNum = atoi(tempStr);
while(tempStr!=NULL){
num = atoi(tempStr);
if(num<minNum)
minNum=num;
tempStr=strtok(NULL," ");
}
printf("%d\n",minNum);
}
return 0;
}
C++ Code:

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <cstdio>
#include <string>

using namespace std;

int main(){
string inputStr,tempStr;
int maxNum,num,cas;
cin>>cas;
getchar(); // To ignore new line
while(cas--){
getline(cin,inputStr);
istringstream token(inputStr);
token>>tempStr;
maxNum = atoi(tempStr.c_str());
while(token){
token >> tempStr;
num = atoi(tempStr.c_str());
if(num>maxNum)
maxNum=num;
}
cout<<maxNum<<endl;
}
return 0;
}
Collected and prepared by
Sudipto Chowdhury Dip
32nd Batch, CSE
Leading University, Sylhet

THANK YOU

You might also like