C语言题目及答案.docx
《C语言题目及答案.docx》由会员分享,可在线阅读,更多相关《C语言题目及答案.docx(86页珍藏版)》请在冰豆网上搜索。
C语言题目及答案
1.ClimbtheTaishanMountain(II)
2.Peach
3.逆序输出数列
4.逆序数
5.计算高度
6.分解质因数
7.输出数字
8.ProblemB:
数字之和
9.换零钱
10.素数求和
11.求阶乘和
11.ProblemF:
求一批正整数中的偶数和
12.统计各种字符个数
13.求最大公约数
14.求最大公约数
15.零起点学算法101——统计字母数字等个数
16.网站泄密
17.老外买瓷砖
18.堆瓷砖
19.新年大酬宾
20.定制瓷砖
21.句子比较大小
22.鹦鹉学舌3——C语言初学者百题大战之十三
23.鹦鹉学舌2——C语言初学者百题大战之十二
24.百鸡问题
25.零起点学算法89——程序设计竞赛
26.零起点学算法89——程序设计竞赛
27.ProblemA:
计算数列和
28.找钱问题
29.Median
30.素月
31.输入任意N个数,求和
32.多项式求和
33.人口问题
34.数字菱形
ClimbtheTaishanMountain(II)
TimeLimit:
1000MSMemoryLimit:
65536K
TotalSubmit:
753Accepted:
495
Description
Labordayiscoming.Sincetheholidayislong,Manystudentsareplanningtohaveatour.TheTaishanMountainisverybeautiful,soWangPengandhisclassmateswanttoclimbtheTaishanMountain.MountaineeringsportsisWangPeng'savocation,soheisveryexcited.Suddenly,averyamusingideacomestohim.TheTaishanMountainhasmanystairs,andhecanclimbthemountainonestepby2stairsorby1stairs.Howmanywaysdoeshehavetoclimbthemountain?
Forexample,thestairsis2,hehas2ways.Thefirstoneisthefirststepby1stair,andthenextstepby1stair.Andthesecondwayisonlyonestepby2stairs.
Input
Theinputisincludingmanytestcases.EverylineisonetestcasewithaintegernumberN(N<=40).NisthestairsoftheTaishanMountain.
Output
YoushouldoutputhowmanywaysWangPengcanclimbthemountain,eachcaseoutputaline.
SampleInput
1
2
3
SampleOutput
1
2
3
#include
intmain()
{
intn,i,j,a[41];
while(scanf("%d",&n)!
=EOF)
{
a[0]=1;
a[1]=2;
for(i=2;i<40;i++)
{
a[i]=a[i-1]+a[i-2];
}
printf("%d\n",a[n-1]);
}
}
Peach
TimeLimit:
1000MSMemoryLimit:
65536K
TotalSubmit:
689Accepted:
495
Description
AlmostallyoungmanknowMonkeySunwhosenameisSunWukong.Onedayhestealsmanypeachesfromthekingdomofheaven.Firstday,heateahalfofthepeaches,thenateanotheroneoftheleftpeaches.Thenextday,heateahalfoftheleftpeaches,thenanotherone.Untilthen-thday,beforeheatehefoundonlyonepeach.
PleasehelpSunWukongtocalculatehowmanypeacheshetookfromthekingdomofheaven.
Input
Theinputfilecontainsoneormoretestcases,eachlineisthedaysN(1Output
Foreachtestcase,outputalineindicatingthenumberofpeachesMonkeySunhadthefirstday.
SampleInput
2
4
SampleOutput
4
22
#include
intmain()
{
intn,t,i,s;
while(scanf("%d",&n)!
=EOF)
{
t=1;
s=0;
for(i=0;i{
s=(t+1)*2;
t=s;
}
printf("%d\n",t);
}
}
循环练习a+b——C语言初学者百题大战之二十二
TimeLimit:
1000MSMemoryLimit:
65536K
TotalSubmit:
3739Accepted:
2513
Description
终于到循环结构了。
现在开始你可以做很多事情了。
现在我们开始第一个循环题目——计算a+b.很多的题目测试数据会有很多组的,一般我们的在线系统没写具体要求的时候,输入是以EOF为结束的。
这题的基本框架如下:
intmain()
{
inta,b;
while(scanf("%d%d",&a,&b)!
=EOF)//特别注意这行的写法
{
...//求和
...//输出
}
}
scanf函数是有返回值的,如果从键盘上成功读入一个数,那么返回值就是1,如果成功读入2个,则返回2。
如果处理到文件末尾,则返回EOF
特别注意:
题目的要求实际上是指每组数据输入结束后,马上输出这组的结果,而不是等所有数据输完后才输出结果
Input
输入为多组测试数据。
每组一行,每行输入2个整数a和b
Output
对于每组测试数据,输出一行,输出a+b的值,直到文件末尾
SampleInput
23
45
78
SampleOutput
5
9
15
#include
intmain()
{
inta,b;
while(scanf("%d%d",&a,&b)!
=EOF)
{
printf("%d\n",a+b);
}
}
新郎新娘
TimeLimit:
1000MSMemoryLimit:
65536K
TotalSubmit:
1659Accepted:
1069
Description
Labordayislong,andmanyyoungpeoplewillhavetheirweddingbetween1st,Mayand7th,May.Yesterday,threeyoungcouplestookphotosbesidetheWestLake.ThethreebridegroomisA,B,C,andthethreebrideisX,Y,Z.Maryaskedthemtoknowwhoarethecouples.Sheaskedthreeofthem.AsaidhewillmarrytoX.XsaidthatheryoungmanisC.
AndCsaidhewillmarrytoZ.Maryknowwhatalltheysaidiswrong.Sinceyouareverysmart,sheasksyoutohelphertofindwhoisthebrideofA,whoisthebrideofB,andwhoisthebrideofC?
Input
NoInput.
Output
Outputthreelines.
thefirstlineisthebrideofA,thesecondlineisthebrideofB,thethirdlineisthebrideofC.
SampleInput
SampleOutput
X
Y
Z
(Maynothesequence)
#include
intmain()
{
intx,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("X\n");
if(y==1)
printf("Y\n");
if(z==1)
printf("Z\n");
if(x==2)
printf("X\n");
if(y==2)
printf("Y\n");
if(z==2)
printf("Z\n");
if(x==3)
printf("X\n");
if(y==3)
printf("Y\n");
if(z==3)
printf("Z\n");
}
}
}
}
最大公约数和最小公倍数
TimeLimit:
1000MSMemoryLimit:
65536K
TotalSubmit:
2745Accepted:
1398
Description
Given2positiveintegerx(1<=x<=1000)andy(1<=y<=1000),youaretocounttheGreatestCommonDivisorandtheLeaseCommonMultipleofxandy.
Input
therearemultitestcases.
xandy,onelineforeachtest.
Output
OutputtheGreatestCommonDivisorandtheLeaseCommonMultipleofxandyinonelineforeachtest.
SampleInput
1218
39
SampleOutput
636
39
Hint
学几个单词:
GreatestCommonDivisor最大公约数
LeaseCommonMultiple最小公倍数
positiveinteger正整数
最大公约数可以用辗转相除法
最小公倍数=x*y/最大公约数
例题:
辗转相除法求gcd(326,78)
326=4×78+14...........(78,14)
78=5×14+8.............(14,8)
14=1×8+6..............(8,6)
8=1×6+2...............(6,2)
6=3×2+0...............(2,0)
所以gcd(326,78)=2
用while循环来实现
while(y!
=0)
{
}
#include
intmain()
{
intx,y,a,b,t,i;
while(scanf("%d%d",&x,&y)!
=EOF)
{
if(x>y)
{
t=x;
x=y;
y=t;
}
for(i=x;i>0;i--)
{
if(x%i==0&&y%i==0)
{
a=i;
break;
}
}
b=(x*y)/a;
printf("%d%d\n",a,b);
}
}
查找某一个数
TimeLimit:
1000MSMemoryLimit:
65536K
TotalSubmit:
1118Accepted:
551
Description
输入一个从小到大排列的有序数列(长度小于100),在此数列中查找某一个数x,若找到,输出相应下标,否则,输出”NotFound".
Input
多组测试数据,先输入要查找的数x和n,再输入n个有序数。
Output
输出x所在位置下标或"NotFound"
SampleInput
28-22389202567
57-223892025
SampleOutput
1
NotFound
#include
intmain()
{
intn,x,a[100],i,s,j;
while(scanf("%d",&x)!
=EOF)
{
scanf("%d",&n);
for(i=0;i{
scanf("%d",&a[i]);
}
s=-1;
for(j=0;j{
if(a[j]==x)
{
printf("%d\n",j);
s=j;
break;
}
}
if(s==-1)
{
printf("NotFound\n");
}
}
}
偶数排序
TimeLimit:
1000MSMemoryLimit:
65536K
TotalSubmit:
630Accepted:
433
Description
输入一个正整数N和N个整数,将它们中的偶数按从大到小的顺序进行排序后输出。
Input
多组测试数据,每组输入一个正整数N(1≤N≤100)和N个整数,用空格分隔。
Output
将这N个数中的偶数按从大到小的顺序输出
SampleInput
108414211304050017100
88020099-1234558811
SampleOutput
500100403014842
200888034-12
#include
intmain()
{
intn,m,j,q,p,i,b[10000],a[10000],r,e;
while(scanf("%d",&n)!
=EOF)
{
for(i=0;i{
scanf("%d",&b[i]);
}
e=0;
for(r=0;r{
if(b[r]%2==0)
{
a[e]=b[r];
e=e+1;
}
}
m=a[0];
for(j=0;j{
for(q=j+1;q{
if(a[q]>a[j])
{
m=a[q];
a[q]=a[j];
a[j]=m;
}
}
}
for(p=0;p{
printf("%d",a[p]);
}
printf("%d\n",a[e-1]);
}
}
N个数从大到小排序
TimeLimit:
1000MSMemoryLimit:
65536K
TotalSubmit:
1787Accepted:
1254
Description
输入一个正整数N和N个整数,将它们按从大到小的顺序进行排序后输出。
Input
多组测试数据,每组输入一个正整数N(1≤N≤100)和N个整数,用空格分隔。
Output
将这N个数按从大到小的顺序重新输出。
SampleInput
10-45128823-920810
512349-2
SampleOutput
882312108520-4-9
12943-2
#include
intmain()
{
intn,m,j,q,p,i,a[10000];
while(scanf("%d",&n)!
=EOF)
{
for(i=0;i{
scanf("%d",&a[i]);
}
m=a[0];
for(j=0;j{
for(q=j+1;q{
if(a[q]>a[j])
{
m=a[q];
a[q]=a[j];
a[j]=m;
}
}
}
for(p=0;p{
printf("%d",a[p]);
}
printf("%d\n",a[n-1]);
}
}
N个数从小到大排序
TimeLimit:
1000MSMemoryLimit:
65536K
TotalSubmit:
2443Accepted:
1289
Description
输入一个正整数N和N个整数,将它们按从小到大的顺序进行排序后输出。
Input
多组测试数据,每组输入一个正整数N(1≤N≤100)和N个整数,用空格分隔。
Output
将这N个数按从小到大的顺序重新输出
SampleInput
10-45128823-920810
512349-2
SampleOutput
-9-4025810122388
-234912
N个数从小到大排序
TimeLimit:
1000MSMemoryLimit:
65536K
TotalSubmit:
2443Accepted:
1289
Description
输入一个正整数N和N个整数,将它们按从小到大的顺序进行排序后输出。
Input
多组测试数据,每组输入一个正整数N(1≤N≤100)和N个整数,用空格分隔。
Output
将这N个数按从小到大的顺序重新输出
SampleInput
10-45128823-920810
512349-2
SampleOutput
-9-4025810122388
-234912
#include
intmain()
{
intn,m,j,q,p,i,a[10000];
while(scanf("%d",&n)!
=EOF)
{
for(i=0;i{
scanf("%d",&a[i]);
}
m=a[0];
for(j=0;j{
for(q=j+1;q{
if(a[q]{
m=a[q];
a[q]=a[j];
a[j]=m;
}
}
}
for(p=0;p{
printf("%d",a[p]);
}
printf("%d\n",a[n-1]);
}
}
逆序输出数列
TimeLimit:
1000MSMemoryLimit:
65536K
TotalSubmit:
345Accepted:
282
Description
输入一个正整数n(n<1<100),再输入n个整数,按逆序输出这些数。
Input
多组测试数据,每组输入一个正整数n,再输入n个整数
Output
按逆序输出这n个数
SampleInput
43528
812345678
3889977
SampleOutput
8253
87654321
779988
#include
intmain()
{
intn,i,j,a[10000];
while(scanf("%d",&n)!
=EOF)
{
for(i=0;i{
scanf("%d",&a[i]);
}
for(j=n-1;j>0;j--)
{
printf("%d",a[j]);
}
printf("%d\n",a[0]);
}
}
逆序数
TimeLimit:
1000MSMemoryLimit:
65536K
TotalSubmit:
544Accepted:
365
Description
输入一个任意整数(int型),输出其位数并逆序输出该数。
Input
多组测试数据,每组输入一个任意整数(int型)
Output
输出其位数及逆序数
SampleInput
12345
-123
SampleOutput
543215
-3213
#include
intmain()
{
intn,w,a[10000],j,i;
while(scanf("%d",&n)!
=EOF)
{
w=n;
if(n<0)
{
n=-n;
}
for(i=0;i<100000;i++)
{
a[i]=n%10;
if(n==0)
{
break;
}
n=n/10;
}
if(w<0)
{
printf("-");
}
for(j=0;j
{
printf("%d",a[j]);
}
printf("%d\n",i);
}
}
计算高度
TimeLimit:
1000MSMemoryLimit:
65536K
TotalSubmit:
519Accepted:
321
Descripti