开始学习ACM的博客【1】——输入与输出

输入与输出——多组数据

ACM的题目中输入与输出的数据一般有多组(不定),而且格式多样。

输入

注:scanf函数

int scanf(const char *format, ...)

返回值:如果成功,该函数返回成功匹配和赋值的个数。如果到达文件末尾或发生读错误,则返回 EOF(-1)。

第一类

sample input

1 5

10 20

output

6

30

此时,输入不说明有多少个input,以EOF为结束标志。

#include <stdio.h>
int main()
{
    int a,b;
    while(scanf("%d %d",&a,&b)!=EOF)
    {
        printf("%d\n",a+b);
    }
    return 0;
}

第二类

sample input

2

1 5

10 20

output

6

30

此时,开头先说明有几组数,再有几次的input。

#include<stdio.h>
int main()
{
    int a;
    scanf("%d",&a);
    for(int i=0;i<a;i++)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        printf("%d\n",x+y);
    }
    return 0;
}

第三类

sample input

1 5

10 20

0 0

output

6

30

此时,不告诉有几组input,以0 0为标志结束。

#include<stdio.h>
int main()
{
    int a,b;
    while(1)
    {
        scanf("%d %d",&a,&b);
        if(a==0&&b==0)
        {
            break;
        }
        printf("%d\n",a+b);
    }
    return 0;
}

 此种写法无法判断是否有足够的数。

#include<stdio.h>
int main()
{
    int a,b;
    while(scanf("%d %d",&a,&b)==2)
    {
        if(a==0&&b==0)break;
        printf("%d\n",a+b);
    }
    return 0;
}

输出

第一类

sample input

1 5

10 20

output

6

30

 一个input对应一个output,中间没有空格

printf("%d\n",a+b);

第二类

sample input

1 5

10 20

output

6

30

每一行输出后面都有一行空格

printf("%d\n\n",a+b);//两个\n

第三类

sample input

1 5

10 20

output

6

30

只有output的两行间才有空格,首尾都没有。

if(n==1)
{printf("%d",a+b);)//将第一行做单独输出,同理也可用于最后一行
else
printf("\n%d",a+b);

OJ评测原理

OJ的评测不需要一次性读入所有的数据和一次性输出所有的数据。(可以读一行输出一行)

OJ会创建一个临时区储存输出的结果,然后逐个对答案进行比较评定。(格式不对也错)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值