ACM输入输出总结

在做题情况中,遇到了不少的输入输出情况,然而有时候对此并不是很熟悉,导致在输入输出的最基本问题上花费了不少的时间,所以利用一点时间,将问题总结一下。

第一种(EOF判断停止)

Input

The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line. 
Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input. 
Sample Input

1 5
10 20

Sample Output

6
30

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;


int main(void){
    int a=0;int b=0;
    while(~scanf("%d %d",&a,&b)){
        printf("%d\n",a+b);
    }

    return 0;

}

 

第二种(给定测试组数)

Input

Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line. 

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input. 
Sample Input

2
1 5
10 20

Sample Output

6
30
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;


int main(void){
    
    int T=0;
    scanf("%d",&T);
    int a=0;int b=0;
    for(int i=0;i<T;i++){
        scanf("%d %d",&a,&b);
        printf("%d\n",a+b);
    }
    
    
    
    return 0;

}

第三种(特殊符号结束)

Input

Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed. 
Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input. 
Sample Input

1 5
10 20
0 0

Sample Output

6
30
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;


int main(void){
    int a=0;int b=0;
    while(~scanf("%d %d",&a,&b)){
        if(a==0&&b==0)    break;
        else{
            printf("%d\n",a+b);
        }
    } 

    return 0;

}

题目里面要求输入特殊情况(本题目是 0 0 时停止),对于不同评测机情况不同,对于本题,要求0 0不能进行处理,所以一定要用else语句套起来。
第四种(每组开头输入个数,特殊符号结束)
Input
Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed. 
Output
For each group of input integers you should output their sum in one line, and with one line of output for each line in input. 
Sample Input
4 1 2 3 4
5 1 2 3 4 5
0 
Sample Output
10
15

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;


int main(void){
    int N=0;
    while(scanf("%d",&N)){
        if(N==0)    break;
        else{
            int a[N+10]={0};
            int sum =0;
            for(int i=0;i<N;i++){
                scanf("%d",&a[i]);
                sum+=a[i];
            }
            printf("%d\n",sum);
        }
    }
    
    return 0;

}

第五种(给定组数与每组测试个数)

Input

Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line. 
Output

For each group of input integers you should output their sum in one line, and with one line of output for each line in input. 
Sample Input

2
4 1 2 3 4
5 1 2 3 4 5

Sample Output

10
15

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;


int main(void){
    int T =0;
    scanf("%d",&T);
    for(int i=0;i<T;i++){
        int N =0;
        scanf("%d",&N);
        int a[N+10]={0};
        int sum=0;
        for(int j=0;j<N;j++){
            scanf("%d",&a[j]);
            sum+=a[j];
        }
        printf("%d\n",sum);
    } 

    return 0;

}

 

第六种(给定测试个数,EOF结束)
Input
Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line. 
Output
For each test case you should output the sum of N integers in one line, and with one line of output for each line in input. 
Sample Input
4 1 2 3 4
5 1 2 3 4 5
Sample Output
10
15

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;


int main(void){
    int N=0;
    while(~scanf("%d",&N)){
        int a[N]={0};
        int sum =0;
        for(int i=0;i<N;i++){
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        printf("%d\n",sum);
    }

    return 0;

}

第七种(输出要求空行)

Input

The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line. 
Output

For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line. 
Sample Input

1 5
10 20

Sample Output

6

30
 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;


int main(void){
    int a=0;int b=0;
    while(~scanf("%d %d",&a,&b)){
        printf("%d\n\n",a+b);
    }

    return 0;

}

第八种(综合上述)

Input

Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line. 
Output

For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs. 
Sample Input

3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3

Sample Output

10

15

6
 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;

int flag = 1;
int main(void){
    int T=0;
    scanf("%d",&T);
    for(int i=0;i<T;i++){
        int N =0;
        scanf("%d",&N);
        int a[N+5]={0};
        int sum=0;
        for(int j=0;j<N;j++){
            scanf("%d",&a[j]);
            sum+=a[j];
        }
        if(i==T-1)    flag=0;
        if(flag) printf("%d\n\n",sum);
        else if(flag==0)    printf("%d\n",sum);
    } 

    return 0;

}

 

总结:
对于之上的几种情况,涵盖了最基本的输入及输出操作。其中比较重要的是EOF的判断,以及特殊符号停止情况的掌握。另外,输出情况中有一种,要求最后一组数据之后不存在其他符号或者操作的情况,一般采用特殊标记的方法

 
posted @ 2018-01-22 21:12  caomp  阅读(655)  评论(0)    收藏  举报