ACM试题及答案

更新时间:2024-04-13 23:16:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

中国石油大学华东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 \programs are not allowed to open and read from/write to files. You will get a \a \

Q: 输入输出需要怎么实现?

A:你的程序必须从stdin(Standard Input标准输入)中读入,将输出数据输出到stdout(Standard Output)。例如,在C语言中使用scanf和printf,在C++语言中使用cin和cout。除了题目要求,不允许输出额外的信息,例如“按任意键退出”、“请输入n的值:”等等。否则将会得到Wrong Answer。 请不要在程序结束时加上system(\等调试信息。 同时,你的程序不允许读取文件或输出到文件中,否则会得到Runtime Error或Wrong Answer。

#include int main() {

int a,b;

scanf(\

printf(\

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(\输入结束时,scanf函数返回值为EOF,即没有数据输入时则退出while循环

{

printf(\

}

return 0; }

您可参考上面给出的示例程序完成本题。

当您在本机上测试时,如果采用标准输入,需要在输入数据结束后手动输入EOF。在Windows下,按下Ctrl-Z,在Linux下,按下Ctrl-D即可。如果采用从文件读入(或重定向输入输出流到文件),则不必在文件结尾添加其他字符。但请注意,在提交代码前请将freopen、fopen等语句注释或删除。 #include

//EOF方法,因为在在线评判中,所有输入都是用文件来模拟输入的,因此EOF方法成为了一种常见方法 int main() {

int a,b;

while(scanf(\ {

printf(\ }

return 0; }

问题 C: A + B Problem II

时间限制: 1 Sec 内存限制: 128 MB 提交: 2638 解决: 2161 [提交][状态][讨论版] 题目描述

给定两个整数a, b,求两个数之和。

输入:输入数据中的第一行是一个正整数T (0<=T<=1000).接下来有T行数据。 每行数据中含有两个整数a, b (-103<=a, b<=103). 输出:对每行数据,输出对应a+b的值。 样例输入 3 1 2

100 100 300 300 样例输出 3 200 600 提示

与Problem 1001不同的是,本题给定了输入数据组数T。 本题仍然需要采用循环读入。下面给出三个示例程序。 示例1:使用for循环,一边读入一边输出

#include

int main() {

int T, a, b, i;

scanf(\

for(int i=0; i

{

scanf(\

printf(\

}

return 0; }

示例2:使用while循环,一边读入一边输出

#include

int main() {

int T, a, b;

scanf(\

while(T--)

{

scanf(\

printf(\

}

return 0; }

示例3:使用for循环,将数据存入数组后再处理。当T较大时,不建议采取此种方法

#include

int main() {

int T, a[1010], b[1010], i;

scanf(\

for(int i=0; i

{

scanf(\

}

for(int i=0; i

{

printf(\

}

return 0; }

#include

//这是典型的计数法,根据给定数值决定输入数量 //方法1 int main() {

scanf(\ sum += tmp; }

printf(\ }

return 0; }

问题 H: A + B Problem VII 题目描述

Your task is to Calculate a + b.

输入

The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.

输出

For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.

样例输入 1 5 10 20 样例输出 6 30

#include int main() {

int a,b;

while(scanf(\ {

printf(\注意,题目中要求每行输出后面加一个空行 }

return 0; }

问题 I: A + B Problem VIII 题目描述

Your task is to calculate the sum of some integers. 输入

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. 输出

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.

样例输入 3

4 1 2 3 4 5 1 2 3 4 5 3 1 2 3 样例输出 10 15 6

#include

//计数法和计数法的复合 int main() {

int num,num2,sum,tmp,i,j;

scanf(\ for (i = 0; i < num; i++) {

scanf(\ sum = 0;

for (j = 0; j < num2; j++) {

scanf(\ sum += tmp; }

printf(\ }

return 0; }

China University of Petroleum

主页 讨论版 问题 状态 排名 9 竞赛&作业 名校联赛 常见问答 [划词翻译 开启] 修改帐号 1503030406 (0) Recent 注销

“端点杯”2015年中国石油大学新生赛敬请关注!

China University of Petroleum Online Judge FAQ

Q:这个在线裁判系统使用什么样的编译器和编译选项?

A:系统运行于Debian/Ubuntu Linux. 使用GNU GCC/G++ 作为C/C++编译器, Free Pascal 作为pascal 编译器 ,用 sun-java-jdk1.6 编译 Java. 对应的编译选项如下:

C: gcc Main.c -o Main -fno-asm -O2 -Wall -lm --static -std=c99 -DONLINE_JUDGE C++: g++ Main.cc -o Main -fno-asm -O2 -Wall -lm --static -DONLINE_JUDGE Pascal: fpc Main.pas -oMain -O1 -Co -Cr -Ct -Ci Java: javac -J-Xms32m -J-Xmx256m Main.java

*Java has 2 more seconds and 512M more memory when running and judging.

编译器版本为(系统可能升级编译器版本,这里直供参考): gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 glibc 2.3.6

Free Pascal Compiler version 2.4.0-2 [2010/03/06] for i386 java version \

Q:程序怎样取得输入、进行输出?

A:你的程序应该从标准输入 stdin('Standard Input')获取输出 并将结果输出到标准输出 stdout('Standard Output').例如,在C语言可以使用 'scanf' ,在C++可以使用'cin' 进行输入;在C使用 'printf' ,在C++使用'cout'进行输出.

用户程序不允许直接读写文件, 如果这样做可能会判为运行时错误 \。

下面是 1000题的参考答案 C++:

#include using namespace std; int main(){ int a,b;

while(cin >> a >> b)

cout << a+b << endl; return 0; } C:

#include int main(){ int a,b;

while(scanf(\ printf(\ return 0;

}

PASCAL:

program p1001(Input,Output); var

a,b:Integer; begin

while not eof(Input) do begin

Readln(a,b); Writeln(a+b); end; end.

Java:

import java.util.*; public class Main{ public static void main(String args[]){ Scanner cin = new Scanner(System.in); int a, b; while (cin.hasNext()){ a = cin.nextInt(); b = cin.nextInt(); System.out.println(a + b); } } }

Q:为什么我的程序在自己的电脑上正常编译,而系统告诉我编译错误! A:GCC的编译标准与VC6有些不同,更加符合c/c++标准:

main 函数必须返回int, void main 的函数声明会报编译错误。 i 在循环外失去定义 \itoa 不是ansi标准函数.

__int64 不是ANSI标准定义,只能在VC使用, 但是可以使用long long声明64位整数。 如果用了__int64,试试提交前加一句#define __int64 long long Q:系统返回信息都是什么意思? A:详见下述:

Pending : 系统忙,你的答案在排队等待.

Pending Rejudge: 因为数据更新或其他原因,系统将重新判你的答案. Compiling : 正在编译.

Running & Judging: 正在运行和判断. Accepted : 程序通过!

Presentation Error : 答案基本正确,但是格式不对。

Wrong Answer : 答案不对,仅仅通过样例数据的测试并不一定是正确答案,一定还有你没想到的地方.

Time Limit Exceeded : 运行超出时间限制,检查下是否有死循环,或者应该有更快的计算方法。

Memory Limit Exceeded : 超出内存限制,数据可能需要压缩,检查内存是否有泄露。

Output Limit Exceeded: 输出超过限制,你的输出比正确答案长了两倍.

Runtime Error : 运行时错误,非法的内存访问,数组越界,指针漂移,调用禁用的系统函数。请点击后获得详细输出。

Compile Error : 编译错误,请点击后获得编译器的详细输出。

Q:如何参加在线比赛?

A:注册 一个帐号,然后就可以练习,点击比赛列表Contests可以看到正在进行的比赛并参加。

其他问题请访问China University of Petroleum论坛系统

本文来源:https://www.bwwdw.com/article/x5kp.html

Top