ACM之输入输出

来源:互联网 发布:java面向对象什么意思 编辑:程序博客网 时间:2024/05/16 18:42

输入_第一类:

输入不说明有多少个Input Block,EOF为结束标志。

1

Description

你的任务是计算a+b

Input

输入包含多行数据,每行有两个整数ab,以空格分开。

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

输入_第二类:

输入一开始就会说有NInput Block,下面接着是NInput Block

2

Description

你的任务是计算a+b

Input

输入包含多行数据,第一行有一个整数N,接下来N行每行有两个整数ab,以空格分开。

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

输入包含多行数据,每行有两个整数ab,以空格分开。测试数据以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