《经典的113道c语言题目及代码解释.docx》由会员分享,可在线阅读,更多相关《经典的113道c语言题目及代码解释.docx(27页珍藏版)》请在第壹文秘上搜索。
1、1 .输入两个正整数,m和n,求其最大公约数和最小公倍数,#inc1.udcvoidmain()Iinthcftint.ini):尸函数声明*/int1.cd(intjntjnt):/函数声明47intu,vhj;Prin1.frP1.easeinput(wonumbers:n*);scanft%d.%dM.&u,&v);h=hcf(u.v):Prin1.frHCF=%dn*h);I=Icd(Uxh);Prin1.T1.C.D=%dJ):)in(hcf(incujntv)IiniUr:if(vu)(t=uu=vv=ujwhi1.e(r=u%v)!=O)(u=vzv=r:)rcturn(v);i
2、m1.cd(i11u.intv.inth)Ircturn(u47);2 .输入一行字符,分别统计出其中字母、空格、数字和其他字符的个数,Winc1.udcintIetteGdigitvSpacc,others;voidmain()Ivoidcount(char11):chartcxS();PrinifCT1.easeinputswing:nM);gc4s(ex1.);Printf(string:5):PUtSaCt);Ieuer-O;digi=O:SP1.KX=0:Othcrs=O;CoUn1.(IeX。;prin1.ftM1.e1.1.er:%d.digi(:,iid.space:%d.o1
3、.hcrs:%(fne.1.e1.ter.digi1.spaceA1.hers):voidcoun1.(charstr()Iinti;if=,asiri(s(ri)=Wstr(!1.1.etter+:c1.scif(sir|iJ=(r&sir|i|3 .输入一个正整数求出它是几位数:输出原数和位数C*inc1.deintdigit:voidmain()Ivoidcount(char();chartext(80:printfC*P1.easeinputnumbcrsn*);gcu(cxt);PriInfrNumbersAn,*);puts(iext);digit=O;count(cx();Pri
4、n1.frdigit%ddigi1.);voidcount(charstr)inii;for(i=0istr(i)!=0,str=,9,)digit;4 .钻入一个正整数.输出原数并逆序打印出各位数字#inc1.udcvoidinvef1.1.ongI11u1.ong);voidnain()Iunsigned1.ongiNumber;PriiHfrP1.easeinputanuinber:nH);scanf(%1.d,.=(Mkx=9)primtr%dn,x);e1.se(printf(,%d.x%10);invcrt1.ongInt(x1.O);J)5 .从键盘上输入假设干学生的一门课成绩,
5、统计并输出最高成绩和最低成绩及相应的序号,当输入负数时结束输入.6 .从键盘上输入假设干学生的一门课成绩,计算出平均分,当输入仪数时结未输入。将结果输出。7 .求1.!+2!+3!+20!.凡结果输出.#inc1.udcvoidmain。If1.oats=O.t=1.;intn;fbr(n=1.;n=20;n+)(t=tn:S=S1H;)Prin1.frI!2!+3!+.+20!-%Cw:8 .打印以以下图案:案Winc1.devoidnain()Iinti,j;PrinIfCThepictureisnw);sia1.iccharpiciurc(4J7=(V7V*a.1t1.t,於,:*,11
6、.I1.1.1.JIfor(i=0;i=3;i+)fortj=0=6)printf(%cvicvoidmain()i11ti.j;Printf(Thepictureis:n*);charpicturc4114=(*hIJ案:*:拿.於产,:,和).fr(i=OJ=3zi+)for(j=OJ=3)printf(%cpicturei);Primfer):J10 .求以下试子的值,112+1314+1/99-1/100.将结果输出。Winc1.udcvoidmain()f1.oatsum=1.0.1.,s=i:inti;for(i三Ui:11 .打印出100999之间的所有水仙花数。析nc1.ude
7、stdi.hvoidnain()inti.j.k.n:Pri1.Mf1.n00799之间的所有水仙花数am:W);fbr(n=100n12 .求Sn=aaa*aaaaaa之tf1.,n,a由雄盘输入C*inc1.devoidmain()Iinta,n.i=1.3n=jn=();prinif(*a.n=:H);SCanfr%d%d”.&a&n):whi1.c(iPrin1.R“d+aa+aaa+.+aa.a-%dn,*.sn):)13 .打印以以下图案:*拿分*衾*衾#inc1.uckvoidmain()JChara1.7=咒叫*.f产);inti.j.k:charspace=,:Prin1.f
8、rThCpicturefbf(i=0u4u+)prinf(n);primf();for(j=1.y=ij+)printftj%c*,space);fortk=0;k7;k+)Prin1.fr%cf1.:JprintfCn);14 .打印以以下图案:112112321123432I15 .打印以以下图案:123432112321121I16 .编写一个统计学生成绩程序,完成以下功能:输入4个学生的2门课成绩:求出全班的总平均分.将结果输出。#inc1.udeWdcfincN4structstudentIcharnum(3:charnamc4;f1.oatscorc2;f1.oatavr;s1.u
9、N:voidmain()Iintij;f1.oatsum,avcragc;fx(i=OiiNii)产输入数据军(Printtrinputscoresofstudcnt%dznJ1.);prinif(*namc:w);$canfC%sM.$tui|.nan)e);fortj=02+)PrinWSCore%d:J+1);scanf(,%stui.scorcyj):Iavcragc=0;/计口/for(i=OiNJ)(SUm=O:fortj=0y2zj)sum=su(i.scorc(j;stui,avr=sun2;average+=stuij.avr:)average/=N;rinfC,Namesc
10、oreIscoe2average”);fr(i=OJNi+)(primf(N%5s%IOsN.stui|.numt$tui.namc);for(j=0rcj);printiV%8.2fn*.stu|i|.avr);Iprinif(,average=%5.2nnM.average);17 .打印以以下图案:*号*8qindudcvoidmain()JChara51=F叫*.*;*:inti,j.k;charspace=,:printftThcpicturei$:w);fb(i=0d5u+)(printf(n):printf(Xfor(j=1.=iy+)p11ntft.space);fortk=0
11、k5k+)printft%c-.a(kh:JPrintfrW);1.&给出年月.t1.计算该H是该年的第几天.#inc1.udevoidI1.1.ainOintsum_day(int.int);int1.cap(intyear);intyear,nnh.day.days;prin1.ft,*inputda1.e(year.mnth.day:*):scanf(od.id.%d*.Aycar.month.day):prinf,%d%d.(d*,ycar.monch.day);days=sum.day(nonh.day);if(kap&mun1.h=3)days=days+1:PrinIfnSthe%dthdayin(hisyear.W,days);intsum-day(intnwnth.mtdayIimdayJabU3三三0,3128,313031.30,31,31,30,3130,31;inti;for(i=1.zin1.hu+)day+=day_tah|i|:rcturn(day);int1.eap(intyear)I