ACM试题及答案.docx
中国石油大学华东ACM试题(黄色高亮为答案)
问题 A: A + B Problem
题目描述
给定两个整数a, b,求两个数之和。
输入
输入仅有一行,两个整数a, b (0<=a, b<=10).
输出
输出 a+b的值。
样例输入
1 2
样例输出
3
提示
Q: Where are the input and the output?
A: Your program shall always read input from stdin (Standard Input) and write output to stdout (Standard Output). For example, you can use 'scanf' in C or 'cin' in C++ to read from stdin, and use 'printf' in C or 'cout' in C++ to write to stdout. You shall not output any extra data to standard output other than that required by the problem, otherwise you will get a "Wrong Answer". User programs are not allowed to open and read from/write to files. You will get a "Runtime Error" or a "Wrong Answer" if you try to do so.
Q: 输入输出需要怎么实现?
A:你的程序必须从stdin(Standard Input标准输入)中读入,将输出数据输出到stdout(Standard Output)。例如,在C语言中使用scanf和printf,在C++语言中使用cin和cout。除了题目要求,不允许输出额外的信息,例如“按任意键退出”、“请输入n的值:”等等。否则将会得到Wrong Answer。 请不要在程序结束时加上system("pause")等调试信息。 同时,你的程序不允许读取文件或输出到文件中,否则会得到Runtime Error或Wrong Answer。
#include
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d\n",a+b);
return 0;
}
问题 B: A + B Problem I
时间限制: 1 Sec 内存限制: 128 MB
提交: 2846 解决: 2191
[提交][状态][讨论版]
题目描述
给定两个整数a, b,求两个数之和。
输入
输入数据有多行.
每行数据中含有两个整数a, b (0<=a, b<=109).
如果对读取输入数据方式产生疑问,请参考HINT。
输出
对每行数据,输出对应a+b的值。
样例输入
123 500
60 80
70 90
样例输出
623
140
160
提示
与Problem 1000不同的是,本题的输入数据要求以EOF作结尾。
EOF即End of File,可以表示文件结尾,也可以表示标准输入的结尾。
在ACM竞赛中,评测采用标准输入输出。当题目中提示“输入以EOF为结束”,或未指明数据组数时,往往无法将数据一次性读入存入数组中,经过计算后再输出。在这种情况下,可以采用以下方式读取数据:
下面给出本题C语言代码示例。
#include
int main()
{
int a,b;
while(scanf("%d %d",&a, &b)!=EOF) // 输入结束时,scanf函数返回值为EOF,即没有数据输入时则退出while循环
{
printf("%d\n",a+b);
}
return 0;
}
您可参考上面给出的示例程序完成本题。
当您在本机上测试时,如果采用标准输入,需要在输入数据结束后手动输入EOF。在Windows下,按下Ctrl-Z,在Linux下,按下Ctrl-D即可。如果采用从文件读入(或重定向输入输出流到文件),则不必在文件结尾添加其他字符。但请注意,在提交代码前请将freopen、fopen等语句注释或删除。
#include
//EOF方法,因为在在线评判中,所有输入都是用文件来模拟输入的,因此EOF方法成为了一种常见方法
int main()
{
int a,b;
while(scanf("%d %d",&a, &b)!=EOF)