ACM试题及答案.docx

上传人:b****5 文档编号:7674746 上传时间:2023-01-25 格式:DOCX 页数:18 大小:20.75KB
下载 相关 举报
ACM试题及答案.docx_第1页
第1页 / 共18页
ACM试题及答案.docx_第2页
第2页 / 共18页
ACM试题及答案.docx_第3页
第3页 / 共18页
ACM试题及答案.docx_第4页
第4页 / 共18页
ACM试题及答案.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

ACM试题及答案.docx

《ACM试题及答案.docx》由会员分享,可在线阅读,更多相关《ACM试题及答案.docx(18页珍藏版)》请在冰豆网上搜索。

ACM试题及答案.docx

ACM试题及答案

中国石油大学华东ACM试题(黄色高亮为答案)

问题A:

A+BProblem

题目描述

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

输入

输入仅有一行,两个整数a,b(0<=a,b<=10).

输出

输出a+b的值。

样例输入

12

样例输出

3

提示

Q:

Wherearetheinputandtheoutput?

A:

Yourprogramshallalwaysreadinputfromstdin(StandardInput)andwriteoutputtostdout(StandardOutput).Forexample,youcanuse'scanf'inCor'cin'inC++toreadfromstdin,anduse'printf'inCor'cout'inC++towritetostdout.Youshallnotoutputanyextradatatostandardoutputotherthanthatrequiredbytheproblem,otherwiseyouwillgeta"WrongAnswer".Userprogramsarenotallowedtoopenandreadfrom/writetofiles.Youwillgeta"RuntimeError"ora"WrongAnswer"ifyoutrytodoso.

Q:

输入输出需要怎么实现?

A:

你的程序必须从stdin(StandardInput标准输入)中读入,将输出数据输出到stdout(StandardOutput)。

例如,在C语言中使用scanf和printf,在C++语言中使用cin和cout。

除了题目要求,不允许输出额外的信息,例如“按任意键退出”、“请输入n的值:

”等等。

否则将会得到WrongAnswer。

请不要在程序结束时加上system("pause")等调试信息。

同时,你的程序不允许读取文件或输出到文件中,否则会得到RuntimeError或WrongAnswer。

#include

intmain()

{

inta,b;

scanf("%d%d",&a,&b);

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

return0;

}

问题B:

A+BProblemI

时间限制:

1Sec内存限制:

128MB

提交:

2846解决:

2191

[提交][状态][讨论版]

题目描述

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

输入

输入数据有多行.

每行数据中含有两个整数a,b(0<=a,b<=109).

如果对读取输入数据方式产生疑问,请参考HINT。

输出

对每行数据,输出对应a+b的值。

样例输入

123500

6080

7090

样例输出

623

140

160

提示

与Problem1000不同的是,本题的输入数据要求以EOF作结尾。

EOF即EndofFile,可以表示文件结尾,也可以表示标准输入的结尾。

在ACM竞赛中,评测采用标准输入输出。

当题目中提示“输入以EOF为结束”,或未指明数据组数时,往往无法将数据一次性读入存入数组中,经过计算后再输出。

在这种情况下,可以采用以下方式读取数据:

下面给出本题C语言代码示例。

#include

intmain()

{

inta,b;

while(scanf("%d%d",&a,&b)!

=EOF)//输入结束时,scanf函数返回值为EOF,即没有数据输入时则退出while循环

{

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

}

return0;

}

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

当您在本机上测试时,如果采用标准输入,需要在输入数据结束后手动输入EOF。

在Windows下,按下Ctrl-Z,在Linux下,按下Ctrl-D即可。

如果采用从文件读入(或重定向输入输出流到文件),则不必在文件结尾添加其他字符。

但请注意,在提交代码前请将freopen、fopen等语句注释或删除。

#include

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

intmain()

{

inta,b;

while(scanf("%d%d",&a,&b)!

=EOF)

{

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

}

return0;

}

问题C:

A+BProblemII

时间限制:

1Sec内存限制:

128MB

提交:

2638解决:

2161

[提交][状态][讨论版]

题目描述

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

输入:

输入数据中的第一行是一个正整数T(0<=T<=1000).接下来有T行数据。

每行数据中含有两个整数a,b(-103<=a,b<=103).

输出:

对每行数据,输出对应a+b的值。

样例输入

3

12

100100

300300

样例输出

3

200

600

提示

与Problem1001不同的是,本题给定了输入数据组数T。

本题仍然需要采用循环读入。

下面给出三个示例程序。

示例1:

使用for循环,一边读入一边输出

#include

intmain()

{

intT,a,b,i;

scanf("%d",&T);

for(inti=0;i

{

scanf("%d%d",&a,&b);

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

}

return0;

}

示例2:

使用while循环,一边读入一边输出

#include

intmain()

{

intT,a,b;

scanf("%d",&T);

while(T--)

{

scanf("%d%d",&a,&b);

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

}

return0;

}

示例3:

使用for循环,将数据存入数组后再处理。

当T较大时,不建议采取此种方法

#include

intmain()

{

intT,a[1010],b[1010],i;

scanf("%d",&T);

for(inti=0;i

{

scanf("%d%d",&a[i],&b[i]);

}

for(inti=0;i

{

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

}

return0;

}

#include

//这是典型的计数法,根据给定数值决定输入数量

//方法1

intmain()

{

intls,a,b,i;

scanf("%d",&ls);

for(i=0;i

{

scanf("%d%d",&a,&b);

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

}

return0;

}

//方法二

intmain()

{

intls,a,b,i;

scanf("%d",&ls);

while(ls--)//利用整数和逻辑类型之间的对应关系

{

scanf("%d%d",&a,&b);

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

}

return0;

}

问题D:

A+BProblemIII

题目描述

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

输入

输入数据有多行。

每行数据中含有两个整数a,b(0<=a,b<=109).

最后一行数据是00,标志着输入结束。

00一行本身不需要计算输出。

如果对读取输入数据方式产生疑问,请参考HINT。

输出

对每行数据,输出对应a+b的值。

样例输入

123500

6080

7090

00

样例输出

623

140

160

提示

与Problem1001相似,本题的输入数据组数未知,但给出了00作为输入的结束符。

您可以据此编制程序。

(提示:

判断a、b的值是否全为0)

题目描述

给定一些整数,对他们求和。

输入

输入数据有多行。

每行数据中第一个整数N(0<=N<=100),后面跟着N个整数ai(-1000<=ai<=1000)

最后一行数据是0,标志着输入结束。

0一行本身不需要计算输出。

输出

对每一行N个正整数ai求和并输出。

样例输入

41234

512345

0

样例输出

10

15

#include

//这种输入被称为哨兵法,选择一个特定的值作为结束标记,也就是一个哨兵,防止你超出范围。

在这个题目中的哨兵就是0

//方法1(推荐方法)

intmain()

{

inta,b;

while

(1)

{

scanf("%d%d",&a,&b);

if(a==0&&b==0)

break;

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

}

return0;

}

//方法2

intmain()

{

inta,b;

scanf("%d%d",&a,&b);//先读一次

while(a!

=0||b!

=0)

{

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

scanf("%d%d",&a,&b);

}

return0;

}

//方法3

intmain()

{

inta,b;

do

{

scanf("%d%d",&a,&b);

if(a==0&&b==0)

break;

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

}while(a!

=0||b!

=0);

return0;

}

//方法4

intmain()

{

inta,b;

while(scanf("%d%d",&a,&b),a!

=0||b!

=0)//利用逗号表达式,逗号表达式从左向右计算,它的返回结果是最后一个表达式的结果

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

return0;

}

问题E:

A+BProblemIV

题目描述

给定一些整数,对他们求和。

输入

输入数据有多行。

每行数据中第一个整数N(0<=N<=100),后面跟着N个整数ai(-1000<=ai<=1000)

最后一行数据是0,标志着输入结束。

0一行本身不需要计算输出。

输出

对每一行N个正整数ai求和并输出。

样例输入

41234

512345

0

样例输出

10

15

#include

//哨兵法和计数法的复合输入

intmain()

{

intnum,sum,tmp,i;

while(scanf("%d",&num),num!

=0)

{

sum=0;

while(num--)

{

scanf("%d",&tmp);

sum+=tmp;

}

printf("%d\n",sum);

}

}

问题F:

A+BProblemV

Yourtaskistocalculatethesumofsomeintegers.

输入

InputcontainsanintegerNinthefirstline,andthenNlinesfollow.EachlinestartswithaintegerM,andthenMintegersfollowinthesameline.

输出

Foreachgroupofinputintegersyoushouldoutputtheirsuminoneline,andwithonelineofoutputforeachlineininput.

样例输入

2

41234

512345

样例输出

10

15

#include

//计数法和计数法的复合

intmain()

{

intnum,num2,sum,tmp,i,j;

scanf("%d",&num);

while(num--)

{

scanf("%d",&num2);

sum=0;

while(num2--)

{

scanf("%d",&tmp);

sum+=tmp;

}

printf("%d\n",sum);

}

return0;

}

问题G:

A+BProblemVI

时间限制:

1Sec内存限制:

128MB

题目描述

Yourtaskistocalculatethesumofsomeintegers.

输入

Inputcontainsmultipletestcases,andonecaseoneline.EachcasestartswithanintegerN,andthenNintegersfollowinthesameline.

输出

ForeachtestcaseyoushouldoutputthesumofNintegersinoneline,andwithonelineofoutputforeachlineininput.

样例输入

41234

512345

#include

//EOF方法和计数法的复合

intmain()

{

intnum,sum,tmp,i;

while(scanf("%d",&num)!

=EOF)

{

sum=0;

for(i=0;i

{

scanf("%d",&tmp);

sum+=tmp;

}

printf("%d\n",sum);

}

return0;

}

问题H:

A+BProblemVII

题目描述

YourtaskistoCalculatea+b.

输入

Theinputwillconsistofaseriesofpairsofintegersaandb,separatedbyaspace,onepairofintegersperline.

输出

Foreachpairofinputintegersaandbyoushouldoutputthesumofaandb,andfollowedbyablankline.

样例输入

15

1020

样例输出

6

30

#include

intmain()

{

inta,b;

while(scanf("%d%d",&a,&b)!

=EOF)

{

printf("%d\n\n",a+b);//注意,题目中要求每行输出后面加一个空行

}

return0;

}

问题I:

A+BProblemVIII

题目描述

Yourtaskistocalculatethesumofsomeintegers.

输入

InputcontainsanintegerNinthefirstline,andthenNlinesfollow.EachlinestartswithaintegerM,andthenMintegersfollowinthesameline.

输出

Foreachgroupofinputintegersyoushouldoutputtheirsuminoneline,andyoumustnotethatthereisablanklinebetweenoutputs.

样例输入

3

41234

512345

3123

样例输出

10

15

6

#include

//计数法和计数法的复合

intmain()

{

intnum,num2,sum,tmp,i,j;

scanf("%d",&num);

for(i=0;i

{

scanf("%d",&num2);

sum=0;

for(j=0;j

{

scanf("%d",&tmp);

sum+=tmp;

}

printf("%d\n\n",sum);

}

return0;

}

ChinaUniversityofPetroleum

主页讨论版问题状态排名9竞赛&作业名校联赛常见问答[划词翻译开启]

修改帐号1503030406(0)Recent注销

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

ChinaUniversityofPetroleumOnlineJudgeFAQ

Q:

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

A:

系统运行于Debian/UbuntuLinux.使用GNUGCC/G++作为C/C++编译器,FreePascal作为pascal编译器,用sun-java-jdk1.6编译Java.对应的编译选项如下:

C:

gccMain.c-oMain-fno-asm-O2-Wall-lm--static-std=c99-DONLINE_JUDGE

C++:

g++Main.cc-oMain-fno-asm-O2-Wall-lm--static-DONLINE_JUDGE

Pascal:

fpcMain.pas-oMain-O1-Co-Cr-Ct-Ci

Java:

javac-J-Xms32m-J-Xmx256mMain.java

*Javahas2moresecondsand512Mmorememorywhenrunningandjudging.

编译器版本为(系统可能升级编译器版本,这里直供参考):

gcc(Ubuntu/Linaro4.4.4-14ubuntu5)4.4.5

glibc2.3.6

FreePascalCompilerversion2.4.0-2[2010/03/06]fori386

javaversion"1.6.0_22"

Q:

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

A:

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

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

下面是1000题的参考答案

C++:

#include

usingnamespacestd;

intmain(){

inta,b;

while(cin>>a>>b)

cout<

return0;

}

C:

#include

intmain(){

inta,b;

while(scanf("%d%d",&a,&b)!

=EOF)

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

return0;

}

PASCAL:

programp1001(Input,Output);

var

a,b:

Integer;

begin

whilenoteof(Input)do

begin

Readln(a,b);

Writeln(a+b);

end;

end.

 

Java:

importjava.util.*;

publicclassMain{

publicstaticvoidmain(Stringargs[]){

Scannercin=newScanner(System.in);

inta,b;

while(cin.hasNext()){

a=cin.nextInt();b=cin.nextInt();

System.out.println(a+b);

}

}

}

spend花费spentspentQ:

为什么我的程序在自己的电脑上正常编译,而系统告诉我编译错误!

A:

GCC的编译标准与VC6有些不同,更加符合c/c++标准:

main函数必须返回int,voidmain的函数声明会报编译错误。

i在循环外失去定义"for(inti=0...){...}"

undo撤消undidundoneitoa不是ansi标准函数.

__int64不是ANSI标准定义,只能在VC使用,但是可以使用longlong声明64位整数。

如果用了__int64,试试提交前加一句#define__int64longlong

Q:

系统返回信息都是什么意思?

split劈开splitsplitA:

详见下述:

Pending:

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

PendingRejudge:

因为数据更新或其他原因,系统将重新判你的答案.

wear穿着worewornCompiling:

正在编译.

sweep打扫sweptsweptRunning&Judging:

正在运行和判断.

Accepted:

程序通过!

dream做梦dreamed/dreamtdreamed/dreamt

PresentationError:

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

 

leave离开leftleftWrongAnswer:

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

TimeLimitExceeded:

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

 

show显露showed

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1