C语言题目及答案.doc_第1页
C语言题目及答案.doc_第2页
C语言题目及答案.doc_第3页
C语言题目及答案.doc_第4页
C语言题目及答案.doc_第5页
已阅读5页,还剩57页未读 继续免费阅读

下载本文档

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

文档简介

目录1. Climb the Taishan Mountain(II)2.Peach3.逆序输出数列4.逆序数5.计算高度6.分解质因数7.输出数字8.Problem B:数字之和9.换零钱10.素数求和11.求阶乘和11.Problem F:求一批正整数中的偶数和12.统计各种字符个数13.求最大公约数14.求最大公约数15.零起点学算法101统计字母数字等个数16.网站泄密17.老外买瓷砖18.堆瓷砖19.新年大酬宾20.定制瓷砖21.句子比较大小22.鹦鹉学舌3C语言初学者百题大战之十三23.鹦鹉学舌2C语言初学者百题大战之十二24.百鸡问题25.零起点学算法89程序设计竞赛26.零起点学算法89程序设计竞赛27.Problem A:计算数列和28.找钱问题29.Median30.素月31.输入任意N个数,求和32.多项式求和33.人口问题34.数字菱形Climb the Taishan Mountain(II)Time Limit:1000MS Memory Limit:65536KTotal Submit:753 Accepted:495DescriptionLabor day is coming.Since the holiday is long,Many students are planning to have a tour. The Taishan Mountain is very beautiful, so WangPeng and his classmates want to climb the Taishan Mountain. Mountaineering sports is WangPengs avocation, so he is very excited. Suddenly, a very amusing idea comes to him. The Taishan Mountain has many stairs, and he can climb the mountain one step by 2 stairs or by 1 stairs. How many ways does he have to climb the mountain? For example, the stairs is 2, he has 2 ways.The first one is the first step by 1 stair, and the next step by 1 stair.And the second way is only one step by 2 stairs. InputThe input is including many test cases. Every line is one test case with a integer number N(N=40). N is the stairs of the Taishan Mountain. OutputYou should output how many ways WangPeng can climb the mountain, each case output a line.Sample Input123Sample Output123#includeint main() int n,i,j,a41; while(scanf(%d,&n)!=EOF) a0=1; a1=2; for(i=2;i40;i+) ai=ai-1+ai-2; printf(%dn,an-1); PeachTime Limit:1000MS Memory Limit:65536KTotal Submit:689 Accepted:495DescriptionAlmost all young man know Monkey Sun whose name is Sun Wukong. One day he steals many peaches from the kingdom of heaven. First day, he ate a half of the peaches, then ate another one of the left peaches.The next day, he ate a half of the left peaches, then another one.Until the n-th day ,before he ate he found only one peach. Please help Sun Wukong to calculate how many peaches he took from the kingdom of heaven.InputThe input file contains one or more test cases, each line is the days N(1 n 30).OutputFor each test case, output a line indicating the number of peaches Monkey Sun had the first day.Sample Input24Sample Output422#includeint main() int n,t,i,s; while(scanf(%d,&n)!=EOF) t=1; s=0; for(i=0;in-1;i+) s=(t+1)*2; t=s; printf(%dn,t); 循环练习a+bC语言初学者百题大战之二十二Time Limit:1000MS Memory Limit:65536KTotal Submit:3739 Accepted:2513Description终于到循环结构了。现在开始你可以做很多事情了。 现在我们开始第一个循环题目计算a+b.很多的题目测试数据会有很多组的,一般我们的在线系统没写具体要求的时候,输入是以EOF为结束的。这题的基本框架如下: int main() int a,b; while(scanf(%d%d,&a,&b)!=EOF) /特别注意这行的写法 ./求和 ./输出 scanf函数是有返回值的,如果从键盘上成功读入一个数,那么返回值就是1,如果成功读入2个,则返回2。如果处理到文件末尾,则返回EOF特别注意:题目的要求实际上是指每组数据输入结束后,马上输出这组的结果,而不是等所有数据输完后才输出结果Input输入为多组测试数据。每组一行,每行输入2个整数a和bOutput对于每组测试数据,输出一行,输出a+b的值,直到文件末尾Sample Input2 34 57 8Sample Output5915#includeint main() int a,b; while(scanf(%d%d,&a,&b)!=EOF) printf(%dn,a+b); 新郎新娘Time Limit:1000MS Memory Limit:65536KTotal Submit:1659 Accepted:1069DescriptionLabor day is long, and many young people will have their wedding between 1st,May and 7th, May. Yesterday, three young couples took photos beside the West Lake. The three bridegroom is A, B, C, and the three bride is X, Y, Z. Mary asked them to know who are the couples.She asked three of them. A said he will marry to X. X said that her young man is C. And C said he will marry to Z. Mary know what all they said is wrong.Since you are very smart, she asks you to help her to find who is the bride of A, who is the bride of B, and who is the bride of C? InputNo Input.OutputOutput three lines. the first line is the bride of A, the second line is the bride of B, the third line is the bride of C.Sample InputSample OutputXYZ(May no the sequence)#includeint main() int x,y,z; for(x=1;x=3;x+) for(y=1;y=3;y+) z=6-x-y; if(x!=y&x!=z&y!=z&x!=3&z!=3&x!=1) if(x=1) printf(Xn); if(y=1) printf(Yn); if(z=1) printf(Zn); if(x=2) printf(Xn); if(y=2) printf(Yn); if(z=2) printf(Zn); if(x=3) printf(Xn); if(y=3) printf(Yn); if(z=3) printf(Zn); 最大公约数和最小公倍数Time Limit:1000MS Memory Limit:65536KTotal Submit:2745 Accepted:1398DescriptionGiven 2 positive integer x(1=x=1000) and y(1=y=1000), you are to count the Greatest Common Divisor and the Lease Common Multiple of x and y.Inputthere are multi test cases. x and y, one line for each test.OutputOutput the Greatest Common Divisor and the Lease Common Multiple of x and y in one line for each test.Sample Input12 183 9Sample Output6 363 9Hint学几个单词: Greatest Common Divisor 最大公约数 Lease Common Multiple 最小公倍数 positive integer 正整数 最大公约数可以用辗转相除法 最小公倍数x*y/最大公约数 例题:辗转相除法求gcd(326,78) 326=478+14.(78,14) 78=514+8.(14,8) 14=18+6.(8,6) 8=16+2.(6,2) 6=32+0.(2,0) 所以gcd(326,78)=2 用while循环来实现 while(y!=0) #includeint main() int x,y,a,b,t,i; while(scanf(%d%d,&x,&y)!=EOF) if(xy) t=x; x=y; y=t; for(i=x;i0;i-) if(x%i=0&y%i=0) a=i; break; b=(x*y)/a; printf(%d %dn,a,b); 查找某一个数Time Limit:1000MS Memory Limit:65536KTotal Submit:1118 Accepted:551Description输入一个从小到大排列的有序数列(长度小于100),在此数列中查找某一个数x,若找到,输出相应下标,否则,输出”Not Found.Input多组测试数据,先输入要查找的数x和n, 再输入n个有序数。Output输出x所在位置下标或Not FoundSample Input2 8 -2 2 3 8 9 20 25 675 7 -2 2 3 8 9 20 25 Sample Output1Not Found#includeint main() int n,x,a100,i,s,j; while(scanf(%d,&x)!=EOF) scanf(%d,&n); for(i=0;in;i+) scanf(%d,&ai); s=-1; for(j=0;jn;j+) if(aj=x) printf(%dn,j); s=j; break; if(s=-1) printf(Not Foundn); 偶数排序Time Limit:1000MS Memory Limit:65536KTotal Submit:630 Accepted:433Description输入一个正整数N和N个整数,将它们中的偶数按从大到小的顺序进行排序后输出。 Input多组测试数据,每组输入一个正整数N(1N100)和N个整数,用空格分隔。 Output将这N个数中的偶数按从大到小的顺序输出Sample Input10 8 4 14 2 11 30 40 500 17 1008 80 200 99 -12 34 55 88 11Sample Output500 100 40 30 14 8 4 2200 88 80 34 -12#includeint main() int n,m,j,q,p,i,b10000,a10000,r,e; while(scanf(%d,&n)!=EOF) for(i=0;in;i+) scanf(%d,&bi); e=0; for(r=0;rn;r+) if(br%2=0) ae=br; e=e+1; m=a0; for(j=0;je-1;j+) for(q=j+1;qaj) m=aq; aq=aj; aj=m; for(p=0;pe-1;p+) printf(%d ,ap); printf(%dn,ae-1); N个数从大到小排序Time Limit:1000MS Memory Limit:65536KTotal Submit:1787 Accepted:1254Description输入一个正整数N和N个整数,将它们按从大到小的顺序进行排序后输出。 Input多组测试数据,每组输入一个正整数N(1N100)和N个整数,用空格分隔。 Output将这N个数按从大到小的顺序重新输出。 Sample Input10 -4 5 12 88 23 -9 2 0 8 105 12 3 4 9 -2 Sample Output88 23 12 10 8 5 2 0 -4 -912 9 4 3 -2#includeint main() int n,m,j,q,p,i,a10000; while(scanf(%d,&n)!=EOF) for(i=0;in;i+) scanf(%d,&ai); m=a0; for(j=0;jn-1;j+) for(q=j+1;qaj) m=aq; aq=aj; aj=m; for(p=0;pn-1;p+) printf(%d ,ap); printf(%dn,an-1); N个数从小到大排序Time Limit:1000MS Memory Limit:65536KTotal Submit:2443 Accepted:1289Description输入一个正整数N和N个整数,将它们按从小到大的顺序进行排序后输出。 Input多组测试数据,每组输入一个正整数N(1N100)和N个整数,用空格分隔。 Output将这N个数按从小到大的顺序重新输出 Sample Input10 -4 5 12 88 23 -9 2 0 8 105 12 3 4 9 -2Sample Output-9 -4 0 2 5 8 10 12 23 88-2 3 4 9 12N个数从小到大排序Time Limit:1000MS Memory Limit:65536KTotal Submit:2443 Accepted:1289Description输入一个正整数N和N个整数,将它们按从小到大的顺序进行排序后输出。 Input多组测试数据,每组输入一个正整数N(1N100)和N个整数,用空格分隔。 Output将这N个数按从小到大的顺序重新输出 Sample Input10 -4 5 12 88 23 -9 2 0 8 105 12 3 4 9 -2Sample Output-9 -4 0 2 5 8 10 12 23 88-2 3 4 9 12#includeint main() int n,m,j,q,p,i,a10000; while(scanf(%d,&n)!=EOF) for(i=0;in;i+) scanf(%d,&ai); m=a0; for(j=0;jn-1;j+) for(q=j+1;qn;q+) if(aqaj) m=aq; aq=aj; aj=m; for(p=0;pn-1;p+) printf(%d ,ap); printf(%dn,an-1); 逆序输出数列Time Limit:1000MS Memory Limit:65536KTotal Submit:345 Accepted:282Description输入一个正整数n(n1100), 再输入n个整数,按逆序输出这些数。Input多组测试数据,每组输入一个正整数n, 再输入n个整数Output按逆序输出这n个数Sample Input4 3 5 2 88 1 2 3 4 5 6 7 83 88 99 77Sample Output8 2 5 38 7 6 5 4 3 2 177 99 88#includeint main() int n,i,j,a10000; while(scanf(%d,&n)!=EOF) for(i=0;i0;j-) printf(%d ,aj); printf(%dn,a0); 逆序数Time Limit:1000MS Memory Limit:65536KTotal Submit:544 Accepted:365Description输入一个任意整数(int型),输出其位数并逆序输出该数。Input多组测试数据,每组输入一个任意整数(int型)Output输出其位数及逆序数Sample Input12345-123Sample Output54321 5-321 3#includeint main() int n,w,a10000,j,i; while(scanf(%d,&n)!=EOF) w=n; if(n0) n=-n; for(i=0;i100000;i+) ai=n%10; if(n=0) break; n=n/10; if(w0) printf(-); for(j=0;ji;j+) printf(%d,aj); printf( %dn,i); 计算高度Time Limit:1000MS Memory Limit:65536KTotal Submit:519 Accepted:321Description一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第n次落地时,共经过多少米?第n次反弹多高?(输出保留1位小数)Input多组测试数据,每组输入一个整数nOutput第n次落下时在空中经过的路程及第n次反弹的高度Sample Input28Sample Outputdistance=200.0 height=25.0distance=298.4 height=0.4#include#includeint main() int n,i; float d,h; while(scanf(%d,&n)!=EOF) h=100.0; d=100.0; for(i=0;in;i+) h=h/2*1.0; if(i!=0) d=d+h*4.0; printf(distance=); printf(%.1f ,d); printf(height=); printf(%.1fn,h); 分解质因数Time Limit:1000MS Memory Limit:65536KTotal Submit:205 Accepted:161Description输入一个正整数,分解质因数。例如,输入90,输出90=2*3*3*5。Input多组测试数据,每组输入一个正整数Output输出该数的质因数Sample Input9012Sample Output90=2*3*3*512=2*2*3#include#includeint main() int n,t,q,o,i,p,j,a10000,w; while(scanf(%d,&n)!=EOF) w=n; o=0; t=2; if(n=1) printf(1=1n); if(n!=1) for(i=0;i100000;i+) if(n%t=0) n=n/t; ao=t; o=o+1; if(n=1) break; if(n%t!=0) for(j=1;j10000;j+) t=t+1; for(q=2;qt;q+) if(t%q=0) break; if(q=t) break; printf(%d=,w); for(p=0;po-1;p+) printf(%d,ap); printf(*); printf(%dn,ao-1); 输出数字Time Limit:1000MS Memory Limit:65536KTotal Submit:766 Accepted:320Description输入一个整数,从高位开始逐位分割并输出。例如输入123456,逐位输出:1,2,3,4,5,6。Input多组测试数据,每组输入一个整数Output逐位输出其各位数字Sample Input123456-789Sample Output1,2,3,4,5,67,8,9#includeint main() int n,i,a1000,j; while(scanf(%d,&n)!=EOF) if(n0) n=-n; for(i=0;i0;j-) printf(%d,aj); printf(%dn,a0); Problem B:数字之和Time Limit:1000MS Memory Limit:65536KTotal Submit:1675 Accepted:810Description输入一个整数,求它的各位数字之和及位数。例如-234的各位数字之和是9,位数是3.Input多组测试数据,每组输入一个整数Output求它的各位数字之和及位数Sample Input-23412345Sample Output9 315 5#includeint main() int a,a1,b; int n,i,j,s,t; while(scanf(%d,&n)!=EOF) if(n0;i+) t=t*10; b=1; s=0; for(j=1;j=i;j+) a=n%(10*b); a1=a/b; b=b*10; s=s+a1; printf(%d %dn,s,i); 换零钱Time Limit:1000MS Memory Limit:65536KTotal Submit:333 Accepted:282Description将一笔钱(大于8分,小于1元)换成1分、2分和5分的硬币(至少各一枚)组合,问有几种换法?Input多组测试数据,每组输入一个大于8分小于100分的钱数Output这笔钱可换成1分、2分和5分的硬币(至少各一枚)的组合数Sample Input81050Sample Output12106#includeint main() int n,m,i,j,t,a1,a2,a3,a4,b4,o; while(scanf(%d,&n)!=EOF) n=n-8; t=1; if(n%2=0&n!=0) t=t+1; if(n%5=0&n!=0) t=t+1; for(a1=1;a1=n-1;a1+) m=n-a1; if(m%2=0&m!=0) t=t+1; for(a2=1;a2=(n-5)/2);a2+) m=n-2*a2; if(m%5=0&m!=0) t=t+1; for(a3=1;a3=(n-5);a3+) m=n-a3; if(m%5=0&m!=0) t=t+1; for(a4=1;a4=(n-7);a4+) m=n-a4; for(b4=1;b4=(m-5)/2);b4+) o=m-2*b4; if(o%5=0&o!=0) t=t+1; printf(%dn,t); 素数求和Time Limit:1000MS Memory Limit:65536KTotal Submit:1525 Accepted:746Description输入一个正整数N和N个正整数,统计其中素数的和。Input多组测试数据,每组输入一个正整数N(1N100)和N个正整数(3),用空格分隔。Output输出所有素数,用空格隔开;再输出这些素数和。 Sample Input10 4 5 8 12 13 24 34 37 20 885 1 5 8 12 13Sample Output5 13 37 s=555 13 s=18#include#includeint main() int N,i,j,a100,b100,q,o=0,s=0,w,t; while(scanf(%d,&N)!=EOF) s=0; o=0; for(i=0;iN;i+) scanf(%d,&ai); for(w=0;wN;w+) for(q=2;qaw;q+) t=aw%q; if(t=0) break; if(t!=0&aw!=1|aw=2) bo=aw; o=o+1; for(j=0;jo;j+) printf(%d ,bj); s=s+bj; prin

温馨提示

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

评论

0/150

提交评论