c語言習題及答案愛課程mooc_第1頁
c語言習題及答案愛課程mooc_第2頁
c語言習題及答案愛課程mooc_第3頁
c語言習題及答案愛課程mooc_第4頁
c語言習題及答案愛課程mooc_第5頁
已閱讀5頁,還剩95頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

第一章includestdio、h>{printfhelloworldn");}無輸出提示信息:"helloworld!\n"dintmain{printf("helloworld!\n");}}輸出樣例:息:lloworldnhellohitnhelloeveryonen輸入樣例:輸出樣例:orldhitloeveryoneintmain{printf("helloworld!\n");printf("hellohit!\n");printf("helloeveryone!\n");}}計算半圓弧的周長及半圓面積(3分)無的面積輸出格式:"Area=%f\n"半圓弧的周長輸出格式:"circumference=%f\n"輸入樣例:intmain{printfAreafn,R*R*PI/2);printf("circumference=%f\n",2*R*PI/2);}}無輸出格式:"volume=%、3f\n"輸入樣例:輸出樣例:intmain{}pleaseinputxandy:輸入提示信息:"pleaseinputxandy:\n"result%、2f\n"輸入樣例:輸出樣例:intmain{printf("pleaseinputxandy:\n");floatx,y;scanf("%f,%f",&x,&y);wy}}y1b輸入提示信息:"Inputx:\n"逆序數(shù)輸出格式:"y=%d\n"逆序數(shù)拆分后的輸出格式:"a=%d,b=%d\n"平方與的輸出格式:"result=%d\n"輸入樣例:輸出樣例:intmain{printf("Inputx:\n");scanf("%d",&x);{x}inta,b,c,d;dx%10;printf("y=%d\n",d*1000+c*100+b*10+a);printf("a=%d,b=%d\n",d*10+c,b*10+a);printf("result=%d\n",(b*10+a)*(b*10+a)+(d*10+c)*(d*10+c));}}大寫(如:Tom)。同時輸出組成該英文名的所有英文字符在26個英文字母中的序號。例:nputyourEnglishnametomt0c輸入提示信息:"inputyourEnglishname:\n"寫的英文姓名的輸出格式:"%c%c%c\n"輸入樣例:輸出樣例:intmain{printf("inputyourEnglishname:\n");chara,b,c;scanf("%c%c%c",&a,&b,&c);printf("%c%c%c\n",a+'A'-'a',b,c);printf("%c:%d\n",a,a-'a'+1);printf("%c:%d\n",b,b-'a'+1);printf("%c:%d\n",c,c-'a'+1);}}計算體指數(shù)(3分)ightweight=140nputweightheightnheightghtdn輸入樣例:輸出樣例:intmain{printf("inputweight,height:\n");scanf("%d,%d",&x,&y);printf("weight=%d\n",x*2);}sum=12,sub=12,multi=0thesecondoperateriszero!sum=-27,sub=-19,multi=92dev=5、75,mod=-3輸入提示信息:"pleaseinputn:\n"數(shù)的輸出格式:"%d,%d\n"法、減法、乘法的輸出格式:"sum=%d,sub=%d,multi=%d\n"輸入樣例:輸出樣例:{mxyprintf("pleaseinputn:\n");scanf("%d",&m);y=m%100;printf("%d,%d\n",x,y);printf("sum=%d,sub=%d,multi=%d\n",x+y,x-y,x*y);ify{}printf("thesecondoperateriszero!\n");}蘇,浙江內(nèi)北京,天津,河北,,河南,安微,湖南,福建,廣東,編寫程序,從鍵盤輸入郵件的目的區(qū)域編碼與重量,計算并輸出運費,計算結果保留2位小數(shù)。計Price:%5、2f\n"輸入樣例:輸出樣例:#include<math、h>intmain{floatweight,price,x1,x2;scanf("%d,%f",&area,&weight);if(weight<=1){}x2=ceil(weight-1);}if(area>0&&area<=4){switchareacase(0):price=x1*10+x2*3;break;case(1):price=x1*10+x2*4;break;case(2):price=x1*15+x2*5;break;ricexxbreakcase(4):price=x1*15+x2*10;break;}}else{printf("ErrorinArea\n");rintfPricen}}}r2563:1000-9999rr6:10-99r3:0-9r輸入提示信息:"Pleaseenterthenumber:\n"間判斷:d0-9999\n"d999\n"d9\n"dn示信息:"error!\n"輸入樣例:輸出樣例:intmain{printf("Pleaseenterthenumber:\n");scanf("%d",&x);輸入樣例:if(x>=1000&&x<=9999)printf("%d:1000-9999\n",x);elseif(x>=0&&x<=9)printf("%d:0-9\n",x);elseif(x>=10&&x<=99)printf("%d:10-99\n",x);elseif(x>=100&&x<=999)printf("%d:100-999\n",x);elseprintf("error!\n");}}c↙輸入提示信息:"Pleaseenterthecoefficientsa,b,c:\n"errorn"4,youhaveenter3-bitsnumber輸出樣例:intmain{floata,b,c,x1,x2,m;printf("Pleaseenterthecoefficientsa,b,c:\n");scanff%f,%f",&a,&b,&c);m=b*b-4*a*c;ifm{printferror\n");}x1=(-b+sqrt(m))/(2*a);x2=(-b-sqrt(m))/(2*a);}}例:orduhaveenterbitsnumber6,youhaveenter2-bitsnumberuhaveenterbitsnumberyouhaveenterbitsnumberyouhaveenterbitsnumber:"%c"輸入提示信息:"Inputyourpassword:\n"如果輸入的就是數(shù)字,輸出格式為:"%c,youhaveenter%d-bitsnumber\n"不就是數(shù)字,輸出提示信息:"error\n"輸入樣例:輸出樣例:}}i})e<=v888e>=SL(}{{5↙21125:5bitsr-12234:5bits輸入提示信息:"Pleaseenterthenumber:\n"判斷該整數(shù)共有幾位:"%d:%dbits\n"輸入樣例:輸出樣例:intmain(){printf("Pleaseenterthenumber:\n");scanf("%d",&x);for(y=1;x/=10;y++);printf("%d:%dbits\n",n,y);}}檢測輸入數(shù)據(jù)中奇數(shù)與偶數(shù)的個數(shù)(4分)是-1,則程序輸出"over!"。否則。用戶每輸入一個數(shù)據(jù),輸出該數(shù)據(jù)就是奇數(shù)還就是偶數(shù),直到用戶輸入-1為止,分別rThetotalnumberofoddis4Thetotalnumberofevenis2rrofoddisofevenis輸入提示信息:"Pleaseenterthenumber:\n"第一個數(shù)據(jù)就就是-1,輸出格式:"over!\n":"%d:odd\n"格式:"%d:even\n"輸入數(shù)據(jù)中奇數(shù)的個數(shù)統(tǒng)計:"Thetotalnumberofoddis%d\n"輸入數(shù)據(jù)中偶數(shù)的個數(shù)統(tǒng)計:"Thetotalnumberofevenis%d\n"輸入樣例:輸出樣例:intmain(){ints,odd=0,even=0;printf("Pleaseenterthenumber:\n");scanf("%d",&s);if(s==-1&&odd==0&&even==0)printf("over!\n");elseif(s%2!=0&&s!=-1){printf("%d:odd\n",s);odd++;}elseif(s%2==0){printf("%d:even\n",s);even++;}elseeven+=0;}while(s!=-1);printf("Thetotalnumberofoddis%d\n",odd);printf("Thetotalnumberofevenis%d\n",even);}}es反彈次數(shù):"%dtimes:\n"utn輸入樣例:輸出樣例:intmain(){imeifloateach=0,sum=0,h=100;printf("input:\n");scanf("%d",&time);for(i=0;i<time;i++){sum+=h;achhsum+=each;}printf("%dtimes:\n",time);}程序存在錯誤,請將其修改正確。并按照下面給出的運行示例檢查程序。include<stdio、h>3.{score11.grade='A’;adeBadeCadeDadeEeaseinputscoreeaseinputscorescoreeaseinputscoregrade:Aeaseinputscoregrade:Beaseinputscoregrade:Ceaseinputscoregrade:Deaseinputscoregrade:E輸入提示信息:"Pleaseinputscore:\n"輸入錯誤提示信息:"Inputerror!\n"grade%c\n"(注意:%c前面有一個空格)輸入樣例:輸出樣例:}a程序運行結果示例:um輸入樣例:輸入樣例:輸出樣例:intmain(){inta,n,sum=0,tem=1;printf("Inputa,n:\n");scanf("%d,%d",&a,&n);for(i=1;i<=n;i++){sum+=(a*tem);tem=tem*10+1;}printf("sum=%d\n",sum);}}序的運行結果示例:menwomenchildren30Inputyear,month:輸出樣例:intmain(){intwoman,man,kid;for(woman=0;woman<=12;woman++){for(man=0;man<=9;man++){kid=36-man-woman;anwomankidprintf("men=%d,women=%d,children=%d\n",man,woman,kid);}}}}編程輸出某年某月有多少天(考慮閏年)。(5分)Inputyear,month:015,3↙sInputyear,month:015,4↙sInputyear,month:016,2↙Inputyear,month:2015,13↙rror息:"Inputyear,month:\n"示信息:"Inputerror!\n"snsnsnintisleap(intn);intmain(){printfInputyearmonthn;scanf("%d,%d",&year,&month);switch(month){case1:casecaseaseasecase0:case12:printf("31days\n");break;ecaseasecase11:printf("30days\n");break;case2:if(isleap(year)){printf("29days\n");break;}else{printf("28days\n");break;}default:printf("Inputerror!\n");}}intisleap(intn){ifnn|n%400==0)eturn}遞歸法計算游戲人員的年齡(4分)"Theperson'sageis%u\n"The_person's_age_is_18The_person's_age_is_28unsignedintAge(unsignedintage);intmain(){unsignedintn=0;scanf("%ud",&n);printf("Theperson'sageis%u\n",Age(n));}unsignedintAge(unsignedintn){ifnreturn10;returnAge(n-1)+2;}}魔術師猜數(shù)(4分)觀眾計算錯誤,魔術師給出的結論:"Thesumyoucalculatediswrong!\n"觀眾計算正確,魔術師給出的結論:"Thenumberis%d\n"alculatediswrongThe_number_is_443請直接拷貝粘貼上面給出的輸入、輸出提示信息與格式控制字符串!intMagic(intm);intmain(){scanf("%d",&m);if(Magic(m))printf("Thenumberis%d\n",Magic(m));printf("Thesumyoucalculatediswrong!\n");}intMagic(intn){for(m=100;m<1000;m++){c=m%10;if((a*122+b*212+c*221)==n)nm}}}"%d%d%d""Theresultis%d\n"The_result_is_12Theresultis_-2與格式控制字符串!intmid(inta,intb,intc);intmain(){scanf("%d%d%d",&a,&b,&c);printf("Theresultis%d\n",mid(a,b,c));}intmid(inta,intb,intc){intm[3]={a,b,c};for(i=0;i<2;i++)for(j=i;j<=2;j++)if(m[j]>m[i])tmj];i}returnm}}還原算術表達式(4分)輸入樣例:輸出樣例:XYZintmid(inta,intb,intc);intmain(){for(x=0;x<9;x++)for(y=0;y<9;y++)for(z=0;z<9;z++)if((x*100+y*110+z*12)==532)printfX%d,Y=%d,Z=%d\n",x,y,z);}計算禮炮聲響次數(shù)(4分)次,C艦每隔7秒放1次。假設各炮手對時間的掌握非常準確,請編程計算觀眾總共可以聽到幾次禮炮聲。輸入樣例:輸出樣例:!intmain{tnfor(t=0;t<=140;t++){if(t%5==0&&t<=21*5){ntinue}if(t%6==0&&t<=21*6){ntinue}if(t%7==0&&t<=21*7){ntinue}}printf("n=%d",n);兔子生崽問題(4分)假設一對小兔的成熟期就是一個月,即一個月可長成成兔,那么如果每對成兔每個月都可以生一對小兔,一對新生的小兔從第二個月起就開始生兔子,試問從一對兔子開始繁殖,一年以后可有多少對兔子(即當年12月份總計有多少對兔子,含成兔與小兔)?請編程求解該問題。d"輸入樣例:輸出樣例:intcreat(intn);intmain{alifor(i=1;i<=12;i++)printf("%4d",creat(i+1));printfnTotaldncreat3));}intcreat(intn){if(n==1||n==2)eturnreturncreat(n-1)+creat(n-2);抓交通肇事犯(4分)一輛卡車違犯交通規(guī)則,撞人后逃跑。現(xiàn)場有三人目擊事件,但都沒記住車號,只記下車號的一些特征。甲說:牌照的前兩位數(shù)字就是相同的;乙說:牌照的后兩位數(shù)字就是相同的,但與前兩位不同;丙就是位數(shù)學家,她說:四位的車號剛好就是一個整數(shù)的平方。現(xiàn)在請根據(jù)以上線索幫助警方找出車號以便盡快破案。k=1000*i+100*i+10*j+j格式:無輸入樣例:輸出樣例:intmain{intab,c,d,i,j;for(i=31;i<100;i++){j=i*i;dj0;if(a==b&&c==d&&a!=c)printf("k=%d,m=%d\n",j,i);}}}檢驗并打印幻方矩陣。(4分)如果就是幻方矩陣,輸出提示信息:"Itisamagicsquare!\n"輸出:"%4d"(換行使用"\n")如果不就是幻方矩陣,輸出提示信息:"Itisnotamagicsquare!\n"23_5_7_14_16_13_20_22tisamagicsquare**17**24***1**8**15**23***5***7**14**16***4***6**13**20**22**10**12**19**21***3**11**18**25***2***91_0_1_6_13_1_1_1_11_1_1_1_19_1_7_1_1Itisnotamagicsquare!!intjudge();jintm[5][5]={0};intmain{for(i=0;i<5;i++)for(j=0;j<5;j++)scanf("%d",&m[i][j]);if(judge()){printf("Itisamagicsquare!\n");for(i=0;i<5;i++){for(j=0;j<5;j++)printf("%4d",m[i][j]);rintfn}}printf("Itisnotamagicsquare!\n");}intjudge(){intlr0;ints[5]={0},t[5]={0};for(i=0;i<5;i++)for(j=0;j<5;j++){s[i]+=m[i][j];t[j]+=m[i][j];if(i==j)l+=m[i][j];if(i+j==4)r+=m[i][j];}if(l!=r)return0;fori;i<5;i++)if(s[i]!=l||t[i]!=l)return0;return}ISBN識別碼判斷(4分)xxN位數(shù)下:BN62-4↙62-7↙62-4請直接拷貝粘貼上面給出的輸入、輸出提示信息與格式控制字符串!輸入樣例:輸入樣例:intij=0;intmain{chars[14],d[14];intm[9]={0};intn0,l=0;getssns-48;for(i=0;s[i]!='\0';i++){if(s[i]!='-'){sij++;}}for(j=1;j<=9;j++){//printf("%4d",m[j-1]-48);l+=(m[j-1]-48)*j;}flnintfRight{strncpy(d,s,12);d[12]=l+48;d[13]='\0';printfs",d);}摘蘋果(4分)100110130200140150156187197149↙49021010215314711013018288113↙6入包括兩行數(shù)據(jù):注意:為避免出現(xiàn)格式錯誤,請直接拷貝粘貼上面給出的輸入、輸出提示信息與格式控制字符串!intmain(){intm[10]={0};ntijnfor(i=0;i<10;i++)scanf("%d",&m[i]);scanf("%d",&n);for(i=0;i<10;i++)if(m[i]<=n+30)j++;printfdj;}}求最大素數(shù)(4分)輸入樣例:輸出樣例:intjudge(intn);intmain(){intm;intj,l=0,sum=0;for(j=500;j>0;j--){if(judge(j))}for(j=0;j<10;j++){sum+=m[j];printf("%6d",m[j]);}printf("\nsum=%d\n",sum);}intjudge(intn){for(i=2;i<=sqrt((double)n);i++){ifn%i==0)}return}}字符串逆序(4分)字符數(shù)組作函數(shù)參數(shù)編程,利用一個數(shù)組實現(xiàn)字符串(允許輸入帶空格的字符串)的逆序存放。要求如下:evoidInversecharstr輸入提示信息:"Inputastring:\n"示信息:"Inversedresults:\n"輸入樣例:輸出樣例:#definemaxn80voidInverse(charstr[]);intmain(){charstr[maxn];printf("Inputastring:\n");getsstrprintfInversedresultsn");}voidInverse(charstr[]){charch;intlen=strlen(str),i;for(i=0;i<len/2;i++){chstr[i];str[i]=str[len-i-1];str[len-i-1]=ch;}}第十章水手分椰子(4分)輸入樣例:輸出樣例:!check(intn){for(i=0;i<5;i++){ifn=1)}return}intmain{for(i=6;i<10000;i++){fchecki{printf("y=%d\n",i);}}}找最值(4分)intFindMaxintnumintnintpMaxPospMaxPos最大值所在的下標程序運行結果示例:234592891278↙Max92,Position=4,Min=-1,Position=0提示信息:"Input10numbers:\n"輸出結果:"Max=%d,Position=%d,Min=%d,Position=%d\n"輸入樣例:輸出樣例:!intFindMax(intnum[],intn,int*pMaxPos);intFindMin(intnum[],intn,int*pMinPos);intnum[10];inti,maxdex=0,mindex=0;intmain(){maxminprintf("Input10numbers:\n");for(i=0;i<10;i++)scanf("%d",&num[i]);max=FindMax(num,10,&m);min=FindMin(num,10,&n);printf("Max=%d,Position=%d,Min=%d,Position=%d\n",m,max,n,min);}intFindMax(intnum[],intn,int*pMaxPos){*pMaxPos=num[0];for(i=1;i<n;i++)if(num[i]>*pMaxPos){*pMaxPos=num[i];}xdex}intFindMin(intnum[],intn,int*pMinPos){*pMinPos=num[0];for(i=1;i<n;i++)if(num[i]<*pMinPos){*pMinPos=num[i];}urnmindex}}星期查找(4分)任意輸入英文的星期幾,通過查找如圖所示的星期表,輸出其對應的數(shù)字,若查到表尾,仍未找到,則輸出錯誤提示信息。目所求。is:"Pleaseenterastring:\n"了,輸出:"%sis%d\n"Notfoundn"輸入樣例:輸出樣例:#definemax50intmain{charweek[7][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};intiflag0;charfind[max];printf("Pleaseenterastring:\n");gets(find);for(i=0;i<7;i++)if(strcmp(week[i],find)==0){flag=1;}ifflag)printf("Notfound!\n");printf("%sis%d\n",find,i);}}楊輝三角形(4分)1111113614111111112345613614201516輸入提示信息:"Inputn(n<=10):\n"式:"%4d""\n"輸入樣例:輸出樣例:!intmain{inti,j,a[10][10];printf("Inputn(n<=10):\n");scanf("%d",&n);for(i=0;i<n;i++)for(j=0;j<n;j++){ai][0]=1;ai[i]=1;}for(i=2;i<n;i++)for(j=1;j<=i-1;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];for(i=0;i<n;i++){for(j=0;j<=i;j++)printf("%4d",a[i][j]);rintfn}}找數(shù)組最值(4分)voidInputArray(int*p,intm,intn);↙2↙ol提示信息:"Inputm,n:\n"數(shù)組維數(shù):"%d,%d"提示信息:"Input%d*%darray:\n""%d""max=%d,row=%d,col=%d\n"輸入樣例:輸出樣例:voidInputArray(int(*p)[10],intm,intn);intFindMax(int(*p)[10],intm,intn,int*pRow,int*pCol);jintnum[10][10];intmain{tmaxintpRowrowint*pCol=&col;printf("Inputm,n:\n");scanf("%d,%d",&m,&n);printf("Input%d*%darray:\n",m,n);InputArray(num,m,n);max=FindMax(num,m,n,pRow,pCol);printf("max=%d,row=%d,col=%d\n",max,row,col);}intFindMax(int(*p)[10],intm,intn,int*pRow,int*pCol){inttem=p[0][0];for(i=0;i<m;i++)for(j=0;j<n;j++)if(p[i][j]>tem){tem=p[i][j];}}voidInputArray(int(*p)[10],intm,intn){for(i=0;i<m;i++)for(j=0;j<n;j++)scanf("%d",&p[i][j]);}}n行n-1遍檢查操作,在每一遍執(zhí)行時,對數(shù)組中剩余的尚未排好序的元較,若排在后面的數(shù)小于排在前面的數(shù),則交換其位置,這樣每一遍操作中都將參與比較的數(shù)中Sortingresults:0123456789輸入數(shù)據(jù)個數(shù)提示:"Inputn:"輸入數(shù)據(jù)提示:"Input%dnumbers:"輸出提示:"Sortingresults:":"%4d"輸入樣例:輸出樣例:jintnum[10000];voidS*m,int*n);intmain{printf("Inputn:");scanf("%d",&n);printf("Input%dnumbers:",n);for(i=0;i<n;i++)scanf("%d",&num[i]);for(i=0;i<n-1;i++)for(j=i;j<n;j++)if(num[j]<num[i])S[j],&num[i]);printf("Sortingresults:");for(i=0;i<n;i++)printf("%4d",num[i]);}voidS*m,int*n){tem=*m;}}刪除字符串中與某字符相同的字符(4分)行結果示例:dgetchar()函數(shù)輸入字符串的提示信息:"Inputastring:\n"個字符的提示信息:"Inputacharacter:\n"出格式:"Results:%s\n"輸入樣例:輸出樣例:#definemaxn1000voiddelete_string(charstr[],charch);intmain{charm[maxn],l[maxn];charn;printf("Inputastring:\n");gets(m);printf("Inputacharacter:\n");rdelete_string(m,n);printf("Results:%s\n",m);}voiddelete_string(charstr[],charch){for(i=j=0;str[i]!='\0';i++)ifstri=ch)str[j++]=str[i];strj='\0';}}求最大數(shù)與最小數(shù)的最大公約數(shù)(4分)↙輸入提示信息:"Input10numbers:\n":"maxNum=%d\n":"minNum=%d\n":"%d"輸入樣例:輸出樣例:#definemaxn10voidS*m,int*n);intgcd(intx,inty);intijintnum[maxn];intmain{maxminprintf("Input10numbers:\n");for(i=0;i<10;i++)scanf("%d",&num[i]);for(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論