正文

ACM basic of input2008-04-06 00:27:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/ieee/33940.html

分享到:

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++ ) {     .... }

阅读(4417) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册