ACMer入门-标准输入与输出格式

ACMer入门-标准输入与输出格式

本文主要帮助刚接触ACM赛制的同学熟悉并理解常见的比赛输入输出格式
由于ACM采用逐字符匹配来判断答案的正确性,所以对格式要求十分严格,为避免思路正确却因为格式问题遭到罚时,需要深刻理解比赛时的数据输入输出与处理。

知识点

1、EOF理解
2、while()循环灵活应用
所有题目链接

例题

A+B for Input-Output Practice (I)

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.

题目分析:

输入ab两个数字,计算a+b每个答案占一行。

思路分析:

由于不清楚具体组数,所以需要判断是否到达文件末尾,主要考大家懂不懂多组输入,理解文件末尾为EOF这个事实。

代码实现:

#include
int main()
{
     
    int a,b;
    //while(~scanf("%d%d",&a,&b))这两个while语句是等价的
    while(scanf("%d%d",&a,&b)!=EOF)//当文件输入到末尾时scanf返回值为 EOF(end of file)意味着多组数据处理结束
    {
     
        printf("%d\n",a+b);//每次答案换一行
    }

}

A+B for Input-Output Practice (II)

Problem Description

Your task is to Calculate a + b.

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.

题目分析:

输入n组数据,输出a+b每个答案占一行

思路分析:

只需要一个值首先获取组数即可

代码实现:

#include
main()
{
     
    int a,b,t;
    scanf("%d",&t);
    while(t--)
    scanf("%d%d",&a,&b),printf("%d\n",a+b);
}

A+B for Input-Output Practice (III)

Problem Description

Your task is to Calculate a + b.

Input

Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.

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.

题目分析:

不确定组数据,输出a+b每个答案占一行,0 0不做处理

思路分析:

将while循环里的EOF判断条件换为a与b其中一个不为0.

代码实现:

#include
main()
{
     
    int a,b;
    while(~scanf("%d%d",&a,&b)&&(a||b))//while(~scanf("%d%d",a+b))
    //a&&b Wrong
    //if(a==0&&b==0)break;
        printf("%d\n",a+b);
}

A+B for Input-Output Practice (IV)

Problem Description

Your task is to Calculate a + b.

Input

Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.

Output

For each group of input integers you should output their sum in one line, and with one line of output for each line in input.

题目分析:

不确定组数据,首先输入一个n代表一行数的个数,如果为0 不做处理,否则读入n个数,求和输出

思路分析:

判断n是否为0,若为0跳出循环。

代码实现:

#include
main()
{
     
    int a,n,s;
    while(scanf("%d",&n),n)
    {
     
        s=0;
        while(n--)
            scanf("%d",&a),s+=a;
        printf("%d\n",s);
    }
}

A+B for Input-Output Practice (V)

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 starts with a integer M, and then M integers follow in the same line.

Output

For each group of input integers you should output their sum in one line, and with one line of output for each line in input.

题目分析:

确定组数数据,首先输入一个t代表有t组数据,再读入m,依次读入m个数。累加求和

思路分析:

按照题目分析照做就好

代码实现:

#include
main()
{
     
    int t,n,x,s;
    scanf("%d",&t);
    while(t--)
    {
     
        s=0;
        scanf("%d",&n);
        while(n--)
            scanf("%d",&x),s+=x;
        printf("%d\n",s);
    }
}

A+B for Input-Output Practice (VI)

Problem Description

Your task is to Calculate a + b.

Input

Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.

Output

For each test case you should output the sum of N integers in one line, and with one line of output for each line in input.

题目分析:

不确定组数数据,多组数据输入,首先输入一个n代表一行有n个数据,读入n个数,求和输出

思路分析:

判断输入是否为EOF

代码实现:

#include
main()
{
     
    int n,x,s;
    while(~scanf("%d",&n))
    {
     
        s=0;
        while(n--)
            scanf("%d",&x),s+=x;
        printf("%d\n",s);
    }
}

A+B for Input-Output Practice (VII)

Problem Description

Your task is to Calculate a + b.

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, and followed by a blank line.

题目分析:

不确定组数数据,多组数据输入,输出a+b每次输出两行

思路分析:

判断输入是否为EOF,注意输出两行

代码实现:

#include
main()
{
     
    int a,b;
    while(~scanf("%d%d",&a,&b))
        printf("%d\n\n",a+b);
}

A+B for Input-Output Practice (VIII)

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 starts with a integer M, and then M integers follow in the same line.

Output

For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.

hint:last line don‘t follow blank line

题目分析:

确定组数数据,输出a+b每次输出两行最后不要输出空行

思路分析

对最后一组数据进行特判断,显然t!=1时输出两行其余不输出。

代码实现:

#include
main()
{
     
    int t,n,x,s;
    scanf("%d",&t);
    while(t--)
    {
     
        s=0;
        scanf("%d",&n);
        while(n--)
            scanf("%d",&x),s+=x;
        printf("%d\n",s);
        if(t!=0)
            puts("");
    }
}

总结:

主要分为两种题型:
1.确定组数 ,使用while(t–)可以快速处理,若要求最后一行不输出空格显然t==1时为最后一组
2.不确定组数,分为判断EOF与特殊数据处理,只需要将break 条件放置while()中即可。

你可能感兴趣的:(ACM教程,C语言,ACM)