输入_第一类: 输入不说明有多少个Input Block,以EOF为结束标志。 例1: Description: 你的任务是计算a+b Input 输入包含多行数据,每行有两个整数a和b,以空格分开。 Output 对于每对整数a,b,输出他们的和,每个和占一行。 Sample Input 1 5 10 20 Sample Output 6 30 #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) { .... } C++语法: while( cin >> a >> b ) { .... } 说明: 1. Scanf函数返回值就是读出的变量个数,如:scanf( “%d %d”, &a, &b ); 如果只有一个整数输入,返回值是1,如果有两个整数输入,返回值是2,如果一个都没有,则返回值是-1。 2. EOF是一个预定义的常量,等于-1。 输入_第二类: 输入一开始就会说有N个Input Block,下面接着是N个Input Block。 例2: Description: 你的任务是计算a+b Input 输入包含多行数据,第一行有一个整数N,接下来N行每行有两个整数a和b,以空格分开。 Output 对于每对整数a,b,输出他们的和,每个和占一行。 Sample Input 2 1 5 10 20 Sample Output 6 30 #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); } } 本类输入解决方案: C语法: scanf("%d",&n) for( i=0 i<n i++ ) { .... } C++语法: cin >> n; for( i=0 i<n i++ ) { .... } 输入_第三类: 输入不说明有多少个Input Block,但以某个特殊输入为结束标志。 例3: Description: 你的任务是计算a+b Input 输入包含多行数据,每行有两个整数a和b,以空格分开。测试数据以0 0结束。 Output 对于每对整数a,b,输出他们的和,每个和占一行。 Sample Input 1 5 10 20 0 0 Sample Output 6 30 #include <stdio.h> int main() { int a,b; while(scanf("%d %d",&a, &b) &&!(a==0 && b==0)) printf("%d\n",a+b); } 本类输入解决方案: C语法: while(scanf("%d",&n) && n!=0 ) { .... } C++语法: while( cin >> n && n != 0 ) { .... } 输入_第四类: 以上几种情况的组合 例4: Description: 你的任务是计算多个数字的和 Input 输入包含多行数据,每行以一个整数N开始,接着有N个整数,以空格分开。测试数据以0结束。 Output 输出每行的N个整数之和,每个和占一行。 Sample Input 4 1 2 3 4 5 1 2 3 4 5 0 Sample Output 10 15 待续…
http://www.baidu.com/s?tn=baiduhome_pg&ie=utf-8&bs=acm+c%2B%2B&f=8&rsv_bp=1&wd=acm+c%2B%2B%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA&rsv_sug3=4&rsv_sug=1&rsv_sug1=4&rsv_sug4=188&inputT=4562