您的位置:首页 > 其它

ACM竞赛之输入输出

2015-07-21 14:30 232 查看
http://acm.njupt.edu.cn/acmhome/problemdetail.do?id=1083&method=showdetail

比赛描述

字符串的输入输出处理。

输入

第一行是一个正整数N,最大为100。

之后输入多行字符串(行数大于N), 每一行字符串可能含有空格,且字符数不超过1000。

输出

对于前N行字符串,按原样输出;

对于其余的字符串以空格符为分割依次按行输出。

注意:每行输出之间均要输出一个空行。

样例输入

2

N U P Ter

样例输出

N

U

P

Ter

提示

对于输入输出仍有困惑的同学请仔细阅读以下内容:

在ACM竞赛中,对于数据的读入,一般有以下四种情况:

一、四种基本输入形式:

1. 单组输入数据

示例:整数求和

http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1001

C语言:

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


C++:

#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
cout<<a+b<<endl;
return 0;
}


注意:输入前无需也不要输出任何提示信息。

2. 多组输入数据,且不说明多少组,直到读至输入文件末尾为止

示例:A + B Problem (1)

http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1084

C语言:

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


说明:scanf函数返回值就是读出的变量个数,如:scanf( “%d %d”, &a, &b );如果只有输入了一个整数,返回值是1,如果输入了两个,返回值是2,如果一个都没有,则返回值是EOF。EOF是一个预定义的常量,等于-1

C++:

#include<iostream>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
cout<<a+b<<endl;
return 0;
}


说明:表达式cin >> m >> n在读入发生错误返回0,否则返回cin的地址。

3. 多组输入数据,不说明多少组,以某特殊输入为结束标志。

示例:A + B Problem (2)

http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1085

C语言:

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


C++:

#include<iostream>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{
if(a==0&&b==0)
break;
cout<<a+b<<endl;
}
return 0;
}


说明:当读入的a与b同时为0时,程序终止;

4. 多组输入数据,开始输入一个T,接下来是T组数据

示例:A + B Problem (3)

http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1086

C语言:

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


C++:

#include<iostream>
using namespace std;
int main()
{
int T;
int a,b;
cin>>T;
while(T--)
{
cin>>a>>b;
cout<<a+b<<endl;
}
return 0;
}


说明:当T组数据处理完后,程序终止;

关于字符串的读入,这里再做专门讨论:

二、字符串输入

  对字符串的输入分三种情况:

  

1、每个字符串中不含空格、制表符及回车

  这种情况,用scanf函数是再好不过的了;

例如:要读入字符串"abcdef"

那么只要:

char str[10];


scanf("%s",str);


说明:scanf函数读入字符串时,是以空格、制表符及回车作为不同字符串之间的分隔符的;

  2、字符串中含有空格、制表符,但不含回车

  对于这种情况不能使用scanf,而应该使用gets函数;

例如:要读入字符串 "Hello world!"

那么只要:

char str[10];

gets(str);


说明:gets函数读入字符串时,只以回车作为不同字符串之间的分隔符;另外,如果要用gets读入多个字符串,可以写成 while(gets(str)){......}

  3、字符串中含回车

  在这种情况下,如果没有题目的说明,程序无法知道哪里是字符串的分界。那么,用scanf("%c",&ch)来读,一边读,一边判断分界条件是否满足,如果满足,则把当前读到的东西存到一个字符串中。

三、输出处理

一般来讲,输出处理一般只有两个问题:空行打印问题与浮点数的精度问题;

1. 关于空行(Blank line)

很多题目都要求在输出数据的恰当位置加空行。一个空行就是一个单独的"\n"。这里,有的题目说:“After each test case, you should output one blank line”,而有的题目说:“Between each test case, you should ouput one blank line”。要注意After和Between的区别,因为如果多了一或少了空行,将导致Presentation Error甚至Wrong Answer。

(1)After

这种情况最简单,只需要输出结果后,再加一个printf("\n"),:

示例:A + B Problem (4)

http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1087

C语言:

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


C++:

#include<iostream>
using namespace std;
int main()
{
int n,sum,a;
while(cin>>n&&n)
{
sum=0;
while(n--)
{
cin>>a;
sum+=a;
}
cout<<sum<<endl;
cout<<endl;
}
return 0;
}


(2)Between

Between和After不同的是,最后一组结果后面不应该再加单独的"\n",应该像这样:

int i;
for (i = 0; i < 10; i++)
{
printf("%d\n", a);
if (i != 9)
printf("\n");
}


由于有时候我们并不知道测试数据有几组(比如测试数据是以end of file 结束的),用上面的方法就不行了,于是,可以换一种写法:

int a;
bool first = true;
while(scanf("%d", &a) == 1)
{
if (!first)
printf("\n");
else
first = false;
printf("%d\n", a);
}


这样,从第二组测试数据起,在输出每组测试数据的结果之前就会输出一个空行,和想要的效果是一样的。

2.关于精度

(1)结果保留x位小数

这种比较简单,只要 printf("%.xf\n",ans); 即可;

例如,要求保留6位小数: printf("%.6f\n",ans);

(2)没有说明要求保留几位,但要求与结果的误差不大于1e-x;

解决 : printf("%.(x+3)f\n",ans);

例如:要求与结果的误差不大于1e-9 : printf("%.12f\n",ans);

题目来源

NUPT
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: