Basic input and output 输入_第一类: 输入不说明有多少个Input Block,以EOF为结束标志。 参见:HDOJ_1089 Problem Description Your task is to Calculate a + b.Too easy?! Of course! I specially designed the problem for acm beginners. You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim. 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 Author lcy Recommend JGShining Hdoj_1089源代码: #include <stdio.h> int main() { int a,b; while(scanf("%d %d",&a, &b) != EOF) printf("%d\n",a+b); } 本类输入解决方案: C语法: while(scanf("%d %d",&a, &b) != EOF) { .... } l C++语法: while( cin >> a >> b ) { .... } 说明(1): Scanf函数返回值就是读出的变量个数,如:scanf( “%d %d”, &a, &b ); 如果只有一个整数输入,返回值是1,如果有两个整数输入,返回值是2,如果一个都没有,则返回值是-1。 EOF是一个预定义的常量,等于-1。 输入_第二类: l 输入一开始就会说有N个Input Block,下面接着是N个Input Block。 参见:HDOJ_1090 Problem Description Your task is to Calculate a + b. 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 Author lcy Recommend JGShining Hdoj_1090源代码: #include <stdio.h> int main() { int n, i, a, b; scanf("%d", &n); for(i=0; i<n; i++) { scanf("%d %d", &a, &b); printf("%d\n", a + b); } } Resultà2 1 5 6 10 20 30 2 1 5 10 20 6 30 本类输入解决方案: l C语法: scanf("%d",&n) ; for( i=0 ; i<n ; i++ ) { .... } l C++语法: cin >> n; for( i=0 ; i<n ; i++ ) { .... }

评论