Input and Output

代码人生写了关于C语言的输入输出,具体内容猛击这里。在这里,我仅对C++的输入cin输出cout做一些小小的总结。具体如下

ACM中最常见的三种输入格式(以A+B为例)

一、hdu1090
Sample Input
2
1 5
10 20

Sample Output
6
30

 

二、hdu1091
Sample Input
1 5
10 20
0 0

Sample Output
6
30 
三、hdu1089
Sample Input
1 5
10 20

Sample Output
6
30

 代码1:

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int T,a,b;
    cin >> T;
    
    for(int i=0; i<T; i++){
        cin >> a >> b;
        cout << a+b <<endl;
    }
    return 0;
}

 

代码2

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int T,a,b;
    cin >> T;
    
    while(T--)
        cin >> a >> b;
        cout << a+b <<endl;
    }
    return 0;
}

 

 代码1

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int a,b;
    
    while(cin>>a >>b){
        if(a==0 && b==0) break;
        cout << a+b <<endl;
    }
    return 0;
}

 

代码2

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int a,b;
    
    while(cin>>a >>b, a || b){
        cout << a+b <<endl;
    }
    return 0;
}

 

代码1 

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int a,b;
    
    while(cin>>a >>b){
        cout << a+b <<endl;
    }
    return 0;
}

 

 

ACM中最常见的三种输出格式

一、hdu1094 一行一输出
NOTE 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
二、hdu1095
NOTE followed by a blank line.                             

Sample Input
1 5
10 20

Sample Output
6

30
三、hdu1096
NOTE 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 <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int T, sum, a;
    
    while(cin>>T){
        sum =0;
        for(int i=0; i<T; i++){
            cin >> a;
            sum += a;
        }
        cout <<sum<<endl;
    }
    return 0;
}

 

 
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int a,b;
    
    while(cin>>a>>b){
        cout <<a+b<<endl<<endl;
    }
    return 0;
}

 

 
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int T, n, a, sum;
    cin >>T;
    while(T--){
        cin >> n;
        sum = 0;
        for(int i=0; i<n; i++){
            cin >> a;
            sum += a;
        }
        cout << sum<<endl;
        if(T) cout <<endl;
    }
    return 0;
}

 

 

 

 

 

 

 

 

 

 

posted @ 2014-06-13 16:29  算法研究  阅读(217)  评论(0)    收藏  举报