Acm试题及答案_第1页
Acm试题及答案_第2页
Acm试题及答案_第3页
Acm试题及答案_第4页
Acm试题及答案_第5页
已阅读5页,还剩85页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选文库 acm 试题及答案1001 sum problem.21089 a+b for input-output practice (i).31090 a+b for input-output practice (ii) .41091 a+b for input-output practice (iii).61092 a+b for input-output practice (iv) .71093 a+b for input-output practice (v).91094 a+b for input-output practice (vi). 111095 a+b for input-

2、output practice (vii).121096 a+b for input-output practice (viii).142000 ascii 码排序 .152001 计算两点间的距离.162002 计算球体积.172003 求绝对值.182004 成绩转换.192005 第几天?.202006 求奇数的乘积.222007 平方和与立方和.232008 数值统计.242009 求数列的和.252010 水仙花数.272011 多项式求和.282012 素数判定.292014 青年歌手大奖赛_评委会打分.312015 偶数求和.322016 数据的交换输出.342017 字符串统计

3、.352019 数列有序! .372020 绝对值排序.382021 发工资咯:).402033 人见人爱 a+b.412039 三角形.432040 亲和数.44精选文库1001 sum problemproblem descriptionh ey, welcome to hdoj(hangzhou dianzi university online judge).i n this problem, your task is to calculate sum(n) = 1 + 2 + 3 + . + n.inputthe input will consist of a series of in

4、tegers n, one integer per line.outputfor each case, output sum(n) in one line, followed by a blank line. you may assume the result will be in the range of 32-bit signed integer.sample input1100sample output15050authordoom iii解答:#includemain()int n,i,sum;sum=0;while(scanf(%d,&n)!=-1)sum=0;for(i=0;i=n

5、;i+)sum+=i;printf(%dnn,sum);精选文库1089 a+b for input-output practice(i)problem descriptionyour task is to calculate a + b.too easy?! of course! i specially designed the problem for acm beginners.you must have found that some problems have the same titles with this one, yes, all these problems were des

6、igned for the same aim.inputthe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.outputfor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.sample i

7、nput1 510 20sample output630authorlcyrecommendjgshining解答:#includemain()int a,b;while(scanf(%d%d,&a,&b)!=eof)printf(%dn,a+b);精选文库1090 a+b for input-output practice(ii)problem descriptionyour task is to calculate a + b.inputinput contains an integer n in the first line, and then n lines follow. each

8、line consists of a pair of integers a and b, separated by a space, one pair of integers per line.outputfor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.sample input21 510 20sample output630authorlcyrecommendj

9、gshining解答:#include#define m 1000void main()int a ,b,n,jm,i;/printf(please input n:n);scanf(%d,&n);for(i=0;in;i+)scanf(%d%d,&a,&b);/printf(%d %d,a,b);ji=a+b;精选文库i=0;while(in)printf(%d,ji); +;iprintf(n);精选文库1091 a+b for input-output practice (iii)problem descriptionyour task is to calculate a + b.inp

10、utinput contains multiple test cases. each test case contains a pair of integers a and b, one pair of integers per line. a test case containing 0 0 terminates the input and this test case is not to be processed.outputfor each pair of input integers a and b you should output the sum of a and b in one

11、 line, and with one line of output for each line in input.sample input1 510 200 0sample output630authorlcyrecommendjgshining解答:#includemain()int a,b;scanf(%d %d,&a,&b);while(!(a=0&b=0)printf(%dn,a+b);scanf(%d %d,&a,&b);精选文库1092 a+b for input-output practice (iv)problem descriptionyour task is to cal

12、culate the sum of some integers.inputinput contains multiple test cases. each test case contains a integer n, and then n integers follow in the same line. a test case starting with 0 terminates the input and this test case is not to be processed.outputfor each group of input integers you should outp

13、ut their sum in one line, and with one line of output for each line in input.sample input4 1 2 3 45 1 2 3 4 50sample output1015authorlcyrecommendjgshining解答:#include int main()int n,sum,i,t;while(scanf(%d,&n)!=eof&n!=0)sum=0;for(i=0;in;i+)scanf(%d,&t);sum=sum+t;精选文库printf(%dn,sum);精选文库1093 a+b for i

14、nput-output practice (v)problem descriptionyour task is to calculate the sum of some integers.inputinput 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.outputfor each group of input integers you should

15、output their sum in one line, and with one line of output for each line in input.sample input24 1 2 3 45 1 2 3 4 5sample output1015authorlcy解答:#includemain()int n,a,b,i,j,sum;sum=0;while(scanf(%dn,&n)!=-1)for(i=0;in;i+)scanf(%d,&b);for(j=0;jb;j+)scanf(%d,&a);sum+=a;精选文库printf(%dn,sum); sum=0;精选文库109

16、4 a+b for input-output practice(vi)problem descriptionyour task is to calculate the sum of some integers.inputinput contains multiple test cases, and one case one line. each case starts with an integer n, and then n integers follow in the same line.outputfor each test case you should output the sum

17、of n integers in one line, and with one line of output for each line in input.sample input4 1 2 3 45 1 2 3 4 5sample output1015authorlcyrecommendjgshining精选文库解答:#includemain()int n,a,b,i,j,sum;sum=0;while(scanf(%dn,&n)!=-1) for(j=0;jn;j+)scanf(%d,&a); sum+=a;printf(%dn,sum); sum=0;精选文库1095 a+b for i

18、nput-output practice (vii)problem descriptionyour task is to calculate a + b.inputthe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.outputfor each pair of input integers a and b you should output the sum of a and b, and followed by a

19、 blank line.sample input1 510 20sample output630authorlcyrecommendjgshining精选文库解答:#includemain()int a,b;while(scanf(%d%d,&a,&b)!=eof)printf(%dnn,a+b);1096 a+b for input-output practice(viii)problem descriptionyour task is to calculate the sum of some integers.inputinput contains an integer n in the

20、first line, and then n lines follow. each line starts with a integer m, and then m integers follow in the same line.outputfor 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.sample input34 1 2 3 45 1 2 3 4 53 1 2 3sam

21、ple output10156authorlcyrecommendjgshining解答:int main()int a,b,i,j,l1000,k;精选文库scanf(%d,&i);getchar();for(j=1;j=i;j+)lj=0;for(j=1;j=i;j+)scanf(%d,&a);getchar();for(k=1;k=a;k+)scanf(%d,&b);getchar();jl+=b;for(j=1;j=i-1;j+)printf(%dnn,lj);printf(%dn,li);2000 ascii 码排序problem description输入三个字符后,按各字符的 a

22、scii 码从小到大的顺序输出这三个字符。 input输入数据有多组,每组占一行,有三个字符组成,之间无空格。 output对于每组输入数据,输出一行,字符中间用一个空格分开。 sample inputqweasdzxcsample outpute q wa d sc x zauthorlcy精选文库sourcec 语言程序设计练习(一)recommendjgshining解答:#includemain()char a,b,c,d;while(scanf(%c %c %c,&a,&b,&c)!=eof)getchar();if(a=b)if(c=a)printf(%c %c %cn,b,a,c

23、);else if(b=c)printf(%c %c %cn,c,b,a);else if(b=b)printf(%c %c %cn,a,b,c);else if(c=a)printf(%c %c %cn,a,c,b);else if(ac)printf(%c %c %cn,c,a,b);2001 计算两点间的距离problem description输入两点坐标(x1,y1),(x2,y2),计算并输出两点间的距离。input输入数据有多组,每组占一行,由 4 个实数组成,分别表示 x1,y1,x2,y2,数据之间用空格隔 开。精选文库output对于每组输入数据,输出一行,结果保留两位小数

24、。sample input0 0 0 10 1 1 0sample output1.001.41authorlcysourcec 语言程序设计练习(一)recommendjgshining解答:#include#includemain()double a,b,c,d,s;while(scanf(%lf %lf %lf %lf,&a,&b,&c,&d)!=eof) s=sqrt(a-c)*(a-c)+(b-d)*(b-d);printf(%.2lfn,s);2002 计算球体积problem description根据输入的半径值,计算球的体积。input输入数据有多组,每组占一行,每行包括一个

25、实数,表示球的半径。 output输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。 精选文库sample input11.5sample output4.18914.137hint#define pi 3.1415927authorlcysourcec 语言程序设计练习(一)recommendjgshining解答:#include#define pi 3.1415927main()double a,v;while(scanf(%lf,&a)!=eof)v=4*pi*a*a*a/3;printf(%.3lfn,v);2003 求绝对值problem description

26、求实数的绝对值。input输入数据有多组,每组占一行,每行包含一个实数。output对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。精选文库sample input123-234.00sample output123.00234.00authorlcysourcec 语言程序设计练习(一)recommendjgshining解答:#includemain()double a;while(scanf(%lf,&a)!=eof)if(a0)a=-a;printf(%.2lfn,a);2004 成绩转换problem description输入一个百分制的成绩 t,将其转换

27、成对应的等级,具体转换规则如下: 90100 为 a;8089 为 b;7079 为 c;6069 为 d;059 为 e;input输入数据有多组,每组占一行,由一个整数组成。output精选文库对于每组输入数据,输出一行。如果输入数据不在 0100 范围内,请输出一行:“score is error!”。sample input5667100123sample outputedascore is error!authorlcysourcec 语言程序设计练习(一)recommendjgshining解答:#include int main()int n;while(scanf(%d,&n)

28、!=eof)if(n100|n=90)printf(an);else if(n=80)printf(bn);else if(n=70)printf(cn);else if(n=60)printf(dn);else printf(en);return 0;2005 第几天?problem description给定一个日期,输出这个日期是该年的第几天。精选文库input输入数据有多组,每组占一行,数据格式为 yyyy/mm/dd 组成,具体参见 sample input , 另外,可以向你确保所有的输入数据是合法的。output对于每组输入数据,输出一行,表示该日期是该年的第几天。sample

29、input1985/1/202006/3/12sample output2071authorlcysourcec 语言程序设计练习(一)recommendjgshining解答:#includemain()int a,b,c,d,e,f,g;while(scanf(%d/%d/%d,&a,&b,&c)!=eof)if(b=1)=dc;else if(b=2)=d31+c;else if(b=3)=d31+28+c;else if(b=4)=d31+28+31+c;else if(b=5)=d31+31+28+30+c;else if(b=6)=d31+28+31+30+31+c;else if

30、(b=7)精选文库=d31+28+31+30+31+30+c;else if(b=8)=d31+28+31+30+31+30+31+c;else if(b=9)=d31+28+31+30+31+30+31+31+c;else if(b=10)=d31+28+31+30+31+30+31+31+30+c;else if(b=11)=d31+28+31+30+31+30+31+31+30+31+c;else if(b=12)=d31+28+31+30+31+30+31+31+30+31+c+30;=ea%100;=fa%400;=ga%4;if(e=0)if(f=0)=1d+d;else=d;el

31、se if(g=0)=dd+1;else=dd;printf(%dn,d);2006 求奇数的乘积problem description给你 n 个整数,求他们中所有奇数的乘积。input输入数据包含多个测试实例,每个测试实例占一行,每行的第一个数为n,表示本组数据一 共有 n 个,接着是 n 个整数,你可以假设每组数据必定至少存在一个奇数。output输出每组数中的所有奇数的乘积,对于测试实例,输出一行。sample input3 1 2 34 2 3 4 5精选文库sample output315authorlcysourcec 语言程序设计练习(一)recommendjgshining解

32、答:#includemain()int n,s,i,a;while(scanf(%d,&n)!=eof)=s1;for(i=0;in;i+)scanf(%d,&a);if(a%2=1)=s*a;else;printf(%dn,s);2007 平方和与立方和problem description给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。input输入数据包含多组测试实例,每组测试实例包含一行,由两个整数 m 和 n 组成。 output对于每组输入数据,输出一行,应包括两个整数x 和 y,分别表示该段连续的整数中所有偶 数的平方和以及所有奇数的立方和。你可以认为 32

33、位整数足以保存结果。精选文库sample input1 32 5sample output4 2820 152authorlcysourcec 语言程序设计练习(一)recommendjgshining解答:#includeint main()int sum1,sum2,n,i,m,t;while(scanf(%d%d,&m,&n)!=eof)sum1=sum2=0;if(mn)t=m;m=n;n=t;for(i=m;i=n;i+)if(i%2=0) sum1+=(i*i);elsesum2+=(i*i*i);printf(%d %dn,sum1,sum2);return 0;2008 数值统

34、计problem description统计给定的 n 个数中,负数、零和正数的个数。input输入数据有多组,每组占一行,每行的第一个数是整数 n(n100),表示需要统计的数值 的个数,然后是 n 个实数;如果 n=0,则表示输入结束,该行不做处理。精选文库output对于每组输入数据,输出一行 a,b 和 c,分别表示给定的数据中负数、零和正数的个数。 sample input6 0 1 2 3 -1 05 1 2 3 4 0.50sample output1 2 30 0 5authorlcysourcec 语言程序设计练习(二)recommendjgshining解答:#includ

35、eint main()int n,i,b1,b2,b3;double a101;while(scanf(%d,&n)!=eof & n!=0)for(i=0;in;i+) scanf(%lf,&ai);b1=b2=b3=0;for(i=0;in;i+)if(ai0) b1+;else (ifai=0) b2+;elseb3+;printf(%d %d %dn,b1,b2,b3);2009 求数列的和精选文库problem description数列的定义如下:数列的第一项为 n,以后各项为前一项的平方根,求数列的前 m 项的和。input输入数据有多组,每组占一行,由两个整数 n(n10000

36、)和 m(m1000)组成,n 和 m 的含 义如前所述。output对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留 2 位小数。 sample input81 42 2sample output94.733.41authorlcysourcec 语言程序设计练习(二)recommendjgshining解答:#include#includemain()double n,m,s,w,i;while(scanf(%lf%lf,&n,&m)!=eof)s=n;for(i=1;im;i+)=nsqrt(n);=s +n;printf(%.2lfn,s);精选文库2010 水仙花数

37、problem description春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的: “水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+33。 现在要求输出所有在 m 和 n 范围内的水仙花数。input输入数据有多组,每组占一行,包括两个整数 m 和 n(100=m=n=999)。output对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须 大于等于 m,并且小于等于 n,如果有多个,则要求从小到大排列在一行内输出,之间用一个 空格隔开;如果给定的范围内不存在水仙花数,则输出 no;每个测

38、试实例的输出占一行。sample input100 120300 380sample outputno370 371authorlcysourcec 语言程序设计练习(二)recommendjgshining解答:#includemain()int m,n,i,w,a,b,c,j,s,d;while(scanf(%d %d,&n,&m)!=eof)=d0;=j1;if(mn)精选文库=mw;=nm;=wn;else;for(i=m;i=n;i+)=ai/100;=bi/10%10;=ci%10;=sa*a*a+b*b*b+c*c*c;if(i=s)if(d!=0)printf( );printf(%d,i);=dd+1;=jj+1;if(j=1)printf(non);elseprintf(n);2011 多项式求和problem description多项式的描述如下:1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + .现在请你求出该多项式的前 n 项的和。input输入数据由 2 行组成,首先是一个正整数 m(m100),表示测试实例的个数,第二行包含 m 个正整数,对于每一个整数(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论