C语言题目及答案Word下载.docx
《C语言题目及答案Word下载.docx》由会员分享,可在线阅读,更多相关《C语言题目及答案Word下载.docx(86页珍藏版)》请在冰豆网上搜索。
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
#include<
stdio.h>
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
689Accepted:
AlmostallyoungmanknowMonkeySunwhosenameisSunWukong.Onedayhestealsmanypeachesfromthekingdomofheaven.Firstday,heateahalfofthepeaches,thenateanotheroneoftheleftpeaches.Thenextday,heateahalfoftheleftpeaches,thenanotherone.Untilthen-thday,beforeheatehefoundonlyonepeach.
PleasehelpSunWukongtocalculatehowmanypeacheshetookfromthekingdomofheaven.
Theinputfilecontainsoneormoretestcases,eachlineisthedaysN(1<
n<
30).
Foreachtestcase,outputalineindicatingthenumberofpeachesMonkeySunhadthefirstday.
4
22
intn,t,i,s;
t=1;
s=0;
for(i=0;
n-1;
s=(t+1)*2;
t=s;
t);
循环练习a+b——C语言初学者百题大战之二十二
3739Accepted:
2513
终于到循环结构了。
现在开始你可以做很多事情了。
现在我们开始第一个循环题目——计算a+b.很多的题目测试数据会有很多组的,一般我们的在线系统没写具体要求的时候,输入是以EOF为结束的。
这题的基本框架如下:
inta,b;
%d%d"
a,&
b)!
=EOF)//特别注意这行的写法
...//求和
...//输出
scanf函数是有返回值的,如果从键盘上成功读入一个数,那么返回值就是1,如果成功读入2个,则返回2。
如果处理到文件末尾,则返回EOF
特别注意:
题目的要求实际上是指每组数据输入结束后,马上输出这组的结果,而不是等所有数据输完后才输出结果
输入为多组测试数据。
每组一行,每行输入2个整数a和b
对于每组测试数据,输出一行,输出a+b的值,直到文件末尾
23
45
78
5
9
15
a+b);
新郎新娘
1659Accepted:
1069
Labordayislong,andmanyyoungpeoplewillhavetheirweddingbetween1st,Mayand7th,May.Yesterday,threeyoungcouplestookphotosbesidetheWestLake.ThethreebridegroomisA,B,C,andthethreebrideisX,Y,Z.Maryaskedthemtoknowwhoarethecouples.Sheaskedthreeofthem.AsaidhewillmarrytoX.XsaidthatheryoungmanisC.
AndCsaidhewillmarrytoZ.Maryknowwhatalltheysaidiswrong.Sinceyouareverysmart,sheasksyoutohelphertofindwhoisthebrideofA,whoisthebrideofB,andwhoisthebrideofC?
NoInput.
Outputthreelines.
thefirstlineisthebrideofA,thesecondlineisthebrideofB,thethirdlineisthebrideofC.
X
Y
Z
(Maynothesequence)
intx,y,z;
for(x=1;
x<
=3;
x++)
for(y=1;
y<
y++)
z=6-x-y;
if(x!
=y&
&
x!
=z&
y!
=3&
z!
=1)
if(x==1)
X\n"
);
if(y==1)
Y\n"
if(z==1)
Z\n"
if(x==2)
if(y==2)
if(z==2)
if(x==3)
if(y==3)
if(z==3)
最大公约数和最小公倍数
2745Accepted:
1398
Given2positiveintegerx(1<
=x<
=1000)andy(1<
=y<
=1000),youaretocounttheGreatestCommonDivisorandtheLeaseCommonMultipleofxandy.
therearemultitestcases.
xandy,onelineforeachtest.
OutputtheGreatestCommonDivisorandtheLeaseCommonMultipleofxandyinonelineforeachtest.
1218
39
636
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)
{
}
intx,y,a,b,t,i;
x,&
y)!
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;
%d%d\n"
a,b);
查找某一个数
1118Accepted:
551
输入一个从小到大排列的有序数列(长度小于100),在此数列中查找某一个数x,若找到,输出相应下标,否则,输出”NotFound"
.
多组测试数据,先输入要查找的数x和n,再输入n个有序数。
输出x所在位置下标或"
NotFound"
28-22389202567
57-223892025
NotFound
intn,x,a[100],i,s,j;
x)!
scanf("
n);
n;
a[i]);
s=-1;
for(j=0;
j<
j++)
if(a[j]==x)
j);
s=j;
}
if(s==-1)
NotFound\n"
偶数排序
630Accepted:
433
输入一个正整数N和N个整数,将它们中的偶数按从大到小的顺序进行排序后输出。
多组测试数据,每组输入一个正整数N(1≤N≤100)和N个整数,用空格分隔。
将这N个数中的偶数按从大到小的顺序输出
108414211304050017100
88020099-1234558811
500100403014842
200888034-12
intn,m,j,q,p,i,b[10000],a[10000],r,e;
b[i]);
e=0;
for(r=0;
r<
r++)
if(b[r]%2==0)
a[e]=b[r];
e=e+1;
m=a[0];
e-1;
for(q=j+1;
q<
e;
q++)
if(a[q]>
a[j])
m=a[q];
a[q]=a[j];
a[j]=m;
for(p=0;
p<
p++)
%d"
a[p]);
a[e-1]);
N个数从大到小排序
1787Accepted:
1254
输入一个正整数N和N个整数,将它们按从大到小的顺序进行排序后输出。
将这N个数按从大到小的顺序重新输出。
10-45128823-920810
512349-2
882312108520-4-9
12943-2
intn,m,j,q,p,i,a[10000];
N个数从小到大排序
2443Accepted:
1289
输入一个正整数N和N个整数,将它们按从小到大的顺序进行排序后输出。
将这N个数按从小到大的顺序重新输出
512349-2
-9-4025810122388
-234912
if(a[q]<
逆序输出数列
345Accepted:
282
输入一个正整数n(n<
1<
100),再输入n个整数,按逆序输出这些数。
多组测试数据,每组输入一个正整数n,再输入n个整数
按逆序输出这n个数
43528
812345678
3889977
8253
87654321
779988
intn,i,j,a[10000];
=EOF)
for(j=n-1;
j>
j--)
a[j]);
a[0]);
逆序数
544Accepted:
365
输入一个任意整数(int型),输出其位数并逆序输出该数。
多组测试数据,每组输入一个任意整数(int型)
输出其位数及逆序数
12345
-123
543215
-3213
intn,w,a[10000],j,i;
w=n;
if(n<
0)
n=-n;
100000;
a[i]=n%10;
if(n==0)
n=n/10;
if(w<
-"
i;
%d\n"
i);
计算高度
519Accepted:
321
Descripti