版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
一、單項選擇題
C語言程序設(shè)計習題解析與實驗指導習題11B 2B 3A 4A 5、C 6、C 7、C 8D 9、B 10C二、填空題1、判斷條件2、基本操作、控制結(jié)構(gòu)3、當型循環(huán)、直到型循環(huán)4、復雜問題的分解和程序模塊的劃分5、編譯、鏈接、運行、調(diào)試s/t->termsum+term->sumi+1->ii<=20?s/t->termsum+term->sumi+1->ii<=20?NY輸出sums->temp,s+t->s,temp->t0.0->sum,2.0->s,1.0->t,0.0->term,0.0->temp,1->i開始結(jié)束1、其迭代方法如下:sum0.0,s2.0,t1.0,term0.0,temp0.0i1;②確定s/t->term;③確定迭代公式sum+term->sum;④通過temp更新迭代變量s,t;i20sum1是迭代法求和的流程圖。圖12、如圖2所示。圖23、如圖3所示。輸入整數(shù)個數(shù)輸入整數(shù)個數(shù)ni<=n?NY輸入整數(shù)aa%2==0?YN輸出sumi+1->isum+a->sum結(jié)束1->i,0->sum開始圖34、如圖4所示。輸入整數(shù)個數(shù)輸入整數(shù)個數(shù)nn%3==0與n%5==0與n%7==0?Y輸出n可同時被3,5,7整除N輸出n不可同時被3,5,7整除開始結(jié)束圖45、如圖5所示。n素數(shù)判規(guī)是當n已知,果n能被2~ 間的意個數(shù)i整,則nnnninnnstep1n值;
n2~
之間的任意一個整數(shù)整除,nstep2環(huán)量值值k= ;nstep3除數(shù)i賦初值i=2;step4kstep5~step6step7;step5nistep7;step6i加1,i=i+1step4;step7n其流程圖和N-S圖如下:開始輸入n開始輸入nNi<=sqrt(n)YYn被i整除Ni=i+1YNi<n輸出n不是素數(shù)輸出n是素數(shù)結(jié)束i=2輸入nK<sqrt(n)fori=2tokn被i整除Y N執(zhí)行break結(jié)束循環(huán)i=i+1Y i<n N輸出n不是素數(shù)輸出n是素數(shù)66
5N-S圖77
6N-S圖year被4整除Y NY year被100整除 NLeap=0year被400整除Y NLeap=1Leap=1Leap=0Y leap N輸出閏年輸出非閏年圖78、如圖8所示。輸入學生數(shù)n輸入學生數(shù)n輸入一個學生成績scorei<=n?YN輸出avsum+score->sum1->i,0.0->sum,0.0->av結(jié)束sum/n->av開始9、略。10、略。一、單項選擇題
圖8習題21->i,0->suma1、D 2、C 3、B 4、B 5、C 6、A 7、A 8、C 9C 10、1->i,0->suma12、C 13D 14、C 15B 16、D 17C 18、C 19、A 20、A 21、A22、D 23C 24、D 25、A二、填空題1、temptemp=aa=bb=cc=temp;25.53、14、0三、編程題1、小寫字母與大寫字母的轉(zhuǎn)換關(guān)系為:小寫字母=大寫字母+32。代碼如下:#include<stdio.h>intmain(){charch;ch=getchar();ch=ch+32;printf("%c\n",ch);return0;}2、代碼如下:#include<stdio.h>#include<math.h>intmain(){longd=1000000,p=20000;doublem,r=0.00545;m=log10(p/(p-d*r))/log10(1+r);printf("%.1f",m);return0;}3、代碼如下:#include<stdio.h>intmain(){intlength,width,height;printf("pleaseenterthelength,widthandheightofacuboid:");scanf("%d%d%d",&length,&width,&height);intarea=0,volume=0;area=length*width*4+width*height*2;volume=length*width*height;printf("%d\n%d\n",area,volume);return0;}4、代碼如下:#nclde<tdio.>itai(){ina,b,c,tep;prnf(enera,b,c”)scaf(“d%d%d”,a,b,c);tep=a;a=bb=cc=tep;prnf(“%d,%d,%d”,a,b,c)return0;}5、代碼如下:#include<stdio.h>intmain(){inta,b,c,d;scanf("%d",&d);a=d/100;b=d/10%10;c=d%10;printf("%d",c*100+b*10+a);return0;}本章實驗題代碼如下:#include<stdio.h>intmain(void){intd,i1,i2,i3,i4,i5,j1,j2,j3,j4,j5,first_sum,second_sum,total;printf("PleaseenterthefirstdigitofaUPC:");scanf("%1d",&d);printf("PleaseenterthefirstgroupoffivedigitsofaUPC:");scanf("%1d%1d%1d%1d%1d",&i1,&i2,&i3,&i4,&i5);printf("PleaseenterthesecondgroupoffivedigitsofaUPC:");scanf("%1d%1d%1d%1d%1d",&j1,&j2,&j3,&j4,&j5);first_sum=d+i2+i4+j1+j3+j5;second_sum=i1+i3+i5+j2+j4;total=3*first_sum+second_sum;printf("Checkdigit:%d\n",9-((total-1)%10));return0;}習題3一、單項選擇題1、D 2、C 3、B 4、D 5、B二、閱讀程序,填程運行結(jié)果124 28,14,5 3、x=2 4、aabb cc 5123,4 6、4294967295 7、13.7000008、a=%d,b=%d 9、a=98, b=765.000000, c=4321.000000本章實驗題代碼如下:#include<stdio.h>intmain(void){inti1,i2,i3,i4;charch;printf("PleaseEnterISBN:");scanf("%1d%c%3d%c%5d%c%1d",&i1,&ch,&i2,&ch,&i3,&ch,&i4);printf("Language:%1d\n",i1);printf("Publisher:%3d\n",i2);printf("BookNumber:%5d\n",i3);printf("Checkdigit:%d\n",i4);return0;}習題4一、單項選擇題1、C 2B 3B 4A 5、A 6、D 7C 8、C 9B 10B 11B 12、D13、B 14C 15、A 16、D 17、B 18C 19、C 20B 21、C 22A 23、D24、B 25B 26、C 27、C 28、D 29、C 30、B二、填空題1、u,5 2、x<y,x<z,y<z三、閱讀程序1、place=4,每位數(shù)字是:8,6,4,2四、編程題1、代碼如下:#include<stdio.h>intmain(void){intgrade,level;scanf("%d",&grade);level=grade/10;printf("Yourscore:");switch(level){case9:printf("A\n");break;case8:printf("B\n");break;case7:printf("C\n");break;case6:printf("D\n");break;case5:case4:case3:case2:case1:case0:printf("E\n");default:printf("inputerror\n");}return0;}2、代碼如下:#include<stdio.h>intmain(void){floatdata1,data2,data3;charch;scanf("%f%c%f",&data1,&ch,&data2);switch(ch){case'+':data3=data1+data2;break;case'-':data3=data1-data2;break;case'*':data3=data1*data2;break;case'/':data3=data1/data2;break;default:printf("error!\n");}printf("%f%c%f=%f\n",data1,ch,data2,data3);return0;}3、代碼如下:#include<stdio.h>intmain(void){intyear;floatprintf("entermoneyandyear=?");scanf("%f%d",&money,&year);if(year==1)rate=0.00315;elseif(year==2)rate=0.00330;elseif(year==3)rate=0.00345;elseif(year==5)rate=0.00375;elseif(year==8)rate=0.00420;elserate=0.0;total=money+money*rate*12*year;printf("Total=%.2f\n",total);return0;}4、代碼如下:#include<stdio.h>intmain(void){floatsalary,rate,tax,money;printf("entersalary:");scanf("%f",&salary);if(salary<1000)rate=0.0;elseif(salary>1000&&salary<=2000)rate=0.05;elseif(salary>2000&&salary<=3000)rate=0.1;elseif(salary>3000&&salary<=6000)rate=0.15;elserate=0.2;tax=salary*rate;money=salary-tax;return0;}5、代碼如下:#include<stdio.h>intmain(void){intn;printf("enterainterger:");scanf("%d",&n);if(n%3==0&&n%5==0&&n%7==0)printf("%dcanbedividedexactlyby3,5,7 elseif(n%3==0&&n%5==0)printf("%dcanbedividedexactlyby3,5 elseif(n%5==0&&n%7==0)printf("%dcanbedividedexactlyby5,7 elseif(n%3==0&&n%7==0)printf("%dcanbedividedexactlyby3,7 elseif(n%3==0)printf("%dcanbedividedexactlyby3.");elseif(n%5==0)printf("%dcanbedividedexactlyby5.");elseif(n%7==0)printf("%dcanbedividedexactlyby7.");elseprintf("%dcannotbedividedexactlybyanyonenumberamong3,5,7.",n);return0;}6、代碼如下:#include<stdio.h>#include<math.h>#defineEPS1e-1intmain(void){floata,b,c;intflag=1;printf("inputa,b,c:");scanf("%f,%f,%f",&a,&b,&c);if(a+b>c&&b+c>a&&a+c>b){if(fabs(a-b)<=EPS&&(fabs(b-c)<=EPS&&fabs(c-a)<=EPS)){printf("等邊");flag=0;}elei(as(b)=E|(ab(-c<=EP||(c-)=ES){printf("等腰");flag=0;}f(bsa*a+*b-*c)=ES|(as(*a+*c*)=ES|as(c+b*b-*a<=E)){printf("直角");flag=0;}if(flag)printf("一般");printf("三角形\n");}elseprintf("不是三角形");return0;}本章實驗題1.身高預測#include<stdio.h>#include<stdlib.h>#include<conio.h>intmain(){floatfaHeight,moHeight,babyHeight;charsex,sport,diet;printf("pleaseinputyoursex:\n");printf("=======================輸入 F 表示女性,M 表示男性=======================\n");scanf("%c",&sex);printf("性別是%c\n",sex);printf("pleaseinputfaHeightandmoHeight:\n");printf("==============================================\n");scanf("%f%f",&faHeight,&moHeight);printf("父親身高是%f,母親身高是%f\n",faHeight,moHeight);fflush(stdin);//刷新緩沖區(qū)printf("Doyoulikesport?\n");printf("======================= 喜 歡 輸 入 Y , 否 則 輸 入N=======================\n");scanf("%c",&sport);printf("\n運動%c\n",sport);fflush(stdin);printf("Doyoueatahealthydiet?\n");printf("======================= 健 康 輸 入 Y , 否 則 輸 入N=======================\n");scanf("%c",&diet);if(sex=='M'){if(sport=='Y'){if(diet=='Y'){飲食健康
}else{}
babyHeight=(faHeight+moHeight)*0.54*(1+0.02)*(1+0.015);//男性,運動,babyHeight=(faHeight+moHeight)*0.54*(1+0.02);//男性,運動,飲食不健康}else{
if(diet=='Y'){康}}else{
}else{}
babyHeight=(faHeight+moHeight)*0.54*(1+0.015);//男性,不運動,飲食健babyHeight=(faHeight+moHeight)*0.54;//男性,不運動,飲食不健康if(sport=='Y'){if(diet=='Y'){babyHeight=((faHeight*0.923+moHeight)/2)*(1+0.02)*(1+0.015);//女性,運動,飲食健康}else{babyHeight=((faHeight*0.923+moHeight)/2)*(1+0.02);//女性,運動,飲食不健康}}else{if(diet=='Y'){食健康}
}else{}
babyHeight=((faHeight*0.923+moHeight)/2)*(1+0.015);//女性,不運動,飲babyHeight=((faHeight*0.923+moHeight)/2);//女性,不運動,飲食不健康}printf("孩子的身高是:%f\n",babyHeight);system("pause");return0;}2.體重正常嗎?#include<stdio.h>intmain(){doublea,b,g,BMI;while(scanf("%lf%lf",&a,&b)!=EOF){g=a/100;BMI=b/(g*g);if(BMI<19)printf("%.2lflower\n",BMI);elseif(BMI>=19&&BMI<25)printf("%.2lfhealth\n",BMI);elseif(BMI>=25&&BMI<30)printf("%.2lfheavy\n",BMI);elseif(BMI>=30&&BMI<40)printf("%.2lfsuperheavy\n",BMI);elseif(BMI>=40)printf("%.2lfextraheavy\n",BMI);}return0;}習題5一、單項選擇題1、A 2、B 、D 4、B二、填空題1、intmain(),i=199;i>=17,i%17==0,i2、t=n%10,max=t3、x%i==0,break4、&n,n/10!=0,n=n/105、b=i/10%10,i==a*a*a+b*b*b+c*c*c6、i=0,f1>200三、編程題1代碼如下:#include<stdio.h>#include<math.h>#defineEPS1e-1intmain(){floatx,y=0;intt;scanf("%f%d",&x,&t);if(t>7&&t<21){if(x<=3)y=0.1;else{y=0.1;do{y=0.1+y;x=x-1;}while(x-3>0);}}else{if(x<=3)y=0.1/2;else{y=0.1/2;do{y=0.1/2+y;x=x-1;}while(x-3>0);}}printf("telephone_fee=%.2f",y);return0;}2、代碼如下:#include<stdio.h>intmain(){5);}
intx,y,z,count=1;printf("可能的兌換方法如下:\n");for(x=0;x<=100;x++)for(y=0;y<=50;y+=2)for(z=0;z<=20;z+=5)if(x+y+z==100)printf(count%4?"%d:1*%d+2*%d+5*%d\t":"%d:1*%d+2*%d+5*%d\n",count++,x,y/2,z/printf("\n\n兌換的方法數(shù)=%d",count-1);return0;3、代碼如下:#include<stdio.h>intmain(){longn,sum,i;while(scanf("%ld",&n)!=EOF){printf1-%ld\n",n);sum=0;for(i=7;i<=n;i++)if(i%7==0)if(i%6==5)if(i%5==4)if(i%3==2){sum++;printf("%ld\n",i);}printf1-%ld%ld\n",n,sum);}}4、代碼如下:intmain(){intx,y,z,number=0;printf(" Men Woman for(x=0;x<=10;x++){y=20-2*x;z=30-x-y;if(3*x+2*y+z==50)printf("%2d:%4d%5d%6d\n",++number,x,y,z);}}5、代碼如下:#include<stdio.h>intmain(){chara,b,c;for(a='x';a<='z';a++)for(b='x';b<='z';b++){if(a!=b)for(c='x';c<='z';c++)if(a!=c&&b!=c){if(a!='x'&&c!='x'&&c!='z')printf("a→%c\nb→%c\nc→%c",a,b,c);}}}本章實驗題1.龜兔賽跑#include<stdio.h>intmain(){intv1,v2,t,s,l;inti,tl=0,rl=0;charcount;scanf("%d%d%d%d%d",&v1,&v2,&t,&s,&l);intf=v1;for(i=1;;i++){if(rl-tl>=t&&v1>0){v1=0;count++;}if(count==s+1){count=0;v1=f;}if(count>0)count++;tl+=v2;rl+=v1;if(tl>=l&&rl>=l){printf("D\n%d\n",i);break;}elseif(tl>=l){printf("T\n%d\n",i);break;}elseif(rl>=l){printf("R\n%d\n",i);break;}}return0;}2、代碼如下:#include<math.h>#include<stdio.h>#defineCONST1.42e8main(){intn;doubleterm=1,sum=1;for(n=2;n<=64;n++){term=term*2;sum=sum+term;}printf("sum=%e\n",sum);printf("volum=%e\n",sum/CONST);}3、代碼如下:#include<stdio.h>#include<time.h>#include<stdlib.h>voidtask1();voidtask2();voidtask3();voidtask4();voidtask5();voidtask6();voidmenu();voidmain(){system("color2f");menu();}voidtask1(){intn,n2anser,esu; /* n,n2anwer*/srand(time(NULL));do{n1=rand()%10+1;n2=rand()%10+1;result=n1+n2;do{printf("%d+%d=n",n1,n2);scanf("%d",&answer);if(result!=answer){printf("Wrong,Pleasetryitagainn");}else{printf("Right!n");}}while(result!=answer);}while(1);}voidtask2(){intn1,n2,answer,result,counter=0; /*counter*/begin:srand(time(NULL));n1=rand()%10+1;n2=rand()%10+1;result=n1+n2;while(counter<=2){printf("%d+%d=?n",n1,n2);scanf("%d",&answer);if(result==answer){printf("Right!n");gotobegin;}else{printf("Wrrong!Pleasetryagain.n");counter++;}}printf("Wrong!Youhavetriedthreetimes!Testover!n");printf("請按1鍵返回主菜單!n");}voidtask3(){intn1,n2,answer,result,i=0,ri=0,wr=0/*ri,wrsrand(time(NULL));while(i<10){n1=rand()%10+1;n2=rand()%10+1;result=n1+n2;printf("%d+%d=n",n1,n2);scanf("%d",&answer);if(result==answer){printf("Rightn");++ri;}else{printf("Wrongn");++wr;}++i;}printf("恭喜!得分%dn",ri*10);printf("正確率%d%%n",ri%10*10);menu();return;}voidtask4(){intn1,n2,counter,score,t; /* scoretif*/floatanswer,result;chara;srand(time(NULL));counter=0;score=0;do{n1=rand()%10+1;n2=rand()%10+1;t=rand()%4;if(t==0){a='+';result=n1+n2;}if(t==1){a='-';result=n1-n2;}if(t==2){a='*';result=n1*n2;}if(t==3){a='/';result=n1/n2;}printf("%d%c%d=?",n1,a,n2);scanf("%f",&answer);counter++;if(answer!=result)printf("wrong!n");else{printf("Right!n");score++;}}while(counter<10);printf("總分為%d,正確率為%d%%",score*10,(score%10)*10);exit(0);}voidtask5(){intn1,n2;inti,counter,t;intresult,answer;charc;srand(time(NULL));for(counter=0;counter<8;counter++){printf("Try!n");for(i=1;i<11;i++){t=rand()%4;n1=rand()%10+1;n2=rand()%10+1;if(t==0){c='+';result=n1+n2;}if(t==1){c='-';result=n1-n2;}if(t==2){c='*';result=n1*n2;}if(t==3){c='/';result=n1/n2;}printf("%d%c%d=n",n1,c,n2);scanf("%d",&answer);if(result!=answer){printf("Wrongn");}else{printf("Right!n");counter=counter+1;}}}}voidtask6(){inti;intn1,n2,data;intresult;intanswer;intchoose1,choose2;floatj,rate;srand(time(NULL));do{ j=0;for(i=1;i<=10;i++){4
n1=rand()%10+1;n2=rand()%10+1;data=rand()%4;switch(data)/*data值產(chǎn)生之后,就需要計算結(jié)果與選擇符號。因為符號一此隨機數(shù)應(yīng)該對4取余,從而得到data值為0到3*/{case0:printf("%d+%d=",n1,n2);result=n1+n2;break;case1:printf("%d-%d=",n1,n2);result=n1-n2;break;case2:printf("%d*%d=",n1,n2);result=n1*n2;break;case3:printf("%d/%d=",n1,n2);result=n1/n2;break;}scanf("%d",&answer);if(answer==result){choose1=rand()%4;switch(choose1)/*當計算正確時,choose1對4取余,隨機得到0-3的數(shù),然后選擇一個回答正確時的夸獎?wù)Z句*/{case0:printf("Verygood!n");break;case1:printf("Excellent!n");break;case2:printf("Nicework!n");break;case3:printf("Keepupthegoodwork!n");break;}j++;}else{的數(shù),然后選擇
choose2=rand()%4;switch(choose2)/*當計算錯誤時,choose2對4取余,隨機得到0-3一個回答錯誤時鼓勵的語句*/{case0:printf("No!Pleasetryagain!n");break;case1:printf("Wrong!Tryoncemore!n");break;case2:printf("Don'tgiveup!n");break;case3:printf("Notcorrect!Keeptrying!n");break;}}}rate=j/10;printf("精準度為:%fn",rate);}while(rate<=0.75);getchar();}voidmenu(){chara;intend;do{printf(" --小生算機學助統(tǒng)--n");printf(" -1.------task1-----------n");printf(" -2.------task2-----------n");printf(" -3.------task3-----------n");printf(" -4.------task4-----------n");printf(" -5.------task5-----------n");printf(" -6.------task6-----------n");printf(" printf(" n");scanf("%c",&a);switch(a){case'1':task1();break;case'2':task2();break;case'3':task3();break;case'4':task4();break;case'5':task5();break;case'6':task6();break;case'7':exit(0);break;default:break;}getchar();scanf("%d",&end);}while(end==1);}習題6一、單項選擇題1、A 2、B 、C 4、A 5、D 6A 、C二、填空題1、i=1,i++,sum=sum+i,fun(100)2、n==2,f(n-1)3、intk,k<500,k%17==0,returnm,fun(500)4、f=n*f(n-1),&n,y5、v=a*b*c,s2=b*c,h,&l,v=vs(l,w,h),三、閱讀程序,寫出運行結(jié)果1、p=272、123453、24、3 7四、編程題1、代碼如下:#include<stdio.h>doublegetSurface(doublelength,doublewidth,doubleheight);doublegetVolume(doublelength,doublewidth,doubleheight);intmain(){//定義長方體的長、寬、高doublelength,width,height,volume,area;printf("enterlength,widthandheightofacube:");scanf("%lf%lf%lf",&length,&width,&height);volume=getVolume(length,width,height);area=getSurface(length,width,height);printf("volume=%.2lf\narea=%.2lf\n",volume,area);return0;}//計算長方體表面積doublegetSurface(doublelength,doublewidth,doubleheight){return(length*width+width*height+length*height);}//計算長方體體積doublegetVolume(doublelength,doublewidth,doubleheight){returnlength*width*height;}2、代碼如下:#include<stdio.h>unsignedlongFac(unsignedintn);intmain(){unsignedlongsum=0;unsignedinti;for(i=1;i<=10;i++)sum+=Fac(i);printf("sum=%ld",sum);return0;}unsignedlongFac(unsignedintn){unsignedlongresult=1;unsignedinti;for(i=1;i<=n;i++)result*=i;returnresult;}3、代碼如下:#include<stdio.h>intdiv(intx);intmain(){inta,y;scanf("%d",&a);y=div(a);printf("%d",y);return0;}intdiv(intx){inty;if(x%5==0&&x%7==0)y=1;elsey=0;return(y);}4、代碼如下:#include<stdio.h>intack(intm,intn){intx;if(m==0&&n==1)x=2;if(m==1&&n==0)x=2;if(m>0&&n>0)x=ack(m-1,ack(m,n-1));returnx;}voidmain(){intm,n;printf("Pleaseinput2intetgers:\n");scanf("%d%d",&m,&n);printf("ack(m,n)=%d\n",ack(m,n));}5、代碼如下:#include<stdio.h>voidkblk(int);voidparse_sort(intnum,int*array);voidmax_min(int*array,int*max,int*min);intcount=0;intmain(){intn;printf("請輸入一個四位數(shù):");scanf("%d",&n);kblk(n);return0;}voidkblk(intnum){intarray[4],max,min;if(num!=6174&&num){parse_sort(num,array);max_min(array,&max,&min);num=max-min;printf("[%d]:%d-%d=%d\n",++count,max,min,num);kblk(num);}}voidparse_sort(intnum,int*array){inti,*j,*k,temp;for(i=0;i<4;i++){j=array+3-i;*j=num%10;num/=10;}for(i=0;i<3;i++)for(j=array,k=array+1;j<array+3-i;j++,k++)if(*j>*k){temp=*j;*j=*k;*k=temp;}return;}voidmax_min(int*array,int*max,int*min){int*i;*min=0;for(i=array;i<array+4;i++)*min=*min*10+*i;*max=0;for(i=array+3;i>=array;i--)*max=*max*10+*i;return;}6、代碼如下:#include<stdio.h>intfish(intn,intx){if((x-1)%5==0){if(n==1)return1;elsereturnfish(n-1,(x-1)/5*4);}return0;}intmain(){inti=0,flag=0,x;do{i=i+1;x=i*5+1;if(fish(5,x)){flag=1;printf("五個人合伙捕到的魚總數(shù)為:%d\n",x);}}while(!flag);}本章實驗題1.小學生計算機輔助教學系統(tǒng)#include<stdio.h>#include<stdlib.h>#include<time.h>intCalulae(it,chro,inty;/alculte(x)op)*/intCeatRanomNmbe); /CreatRanomNmbe-10*/charCreateRandomOperator(); CreateRandomOperator((*、/)*/voidPrintRandomRightEvaluation(PrintRandomRightEvaluation(),生成*/voidPrintRandomWrongEvaluation(PrintRandomWrongEvaluation(),生成*/intmain(){inta,b,userAnswer,i,rightNumber=0,flag=0;/*a,b,rightNumber751*/charopChar; /*opChar*/srand(time(NULL)); */do{for(i=0;i<10;i++) /*1010*/{a*/b*/
a=CreateRandomNumber(); /*b=CreateRandomNumber(); /*產(chǎn)一個機數(shù)給整型量opChar=CreateRandomOperator(); /*產(chǎn)生一隨機算賦值給符變量opChar*/printf("%d%c%d=?\n",a,opChar,b); /*輸一四運式=?*/scanf("%d",&userAnswer); /*用輸答案*/if(userAnswer==Calculate(a,opChar,b))/*如果答案正確*/{PrintRandomRightEvaluation(); /*輸一題做機示*/rightNumber++; /*回正題加1*/}else /**/{PrintRandomWrongEvaluation(); /**/}}printf("Totalscoreis%d\n",rightNumber*10);printf("Rateofcorrectnessis%d%%\n",rightNumber*10);if(rightNumber*10<75) /*75{printf("OnceAgain!\n");rightNumber=0; /**/flag=1;}}while(flag);return0;}/*(x,)(p*/intCalculate(intx,charop,inty){switch(op){case'+':returnx+y;case'-':returnx-y;case'*':returnx*y;case'/':returnx/y;default:printf("Operatorerror!\n");return0;}}/*1-10*/intCreateRandomNumber(){returnrand()%10+1;}/*函數(shù)功能:隨機生成一個運算符號(+,-,*,/)*/charCreateRandomOperator(){intop;op=rand()%4+1;switch(op){case1:return'+';case2:return'-';case3:return'*';case4:return'/';}return0;}/*函數(shù)功能:生成一個題目做對的隨機提示*/voidPrintRandomRightEvaluation(){inti;i=rand()%4+1;switch(i){case1:printf("Verygood!\n");break;case2:printf("Excellent!\n");break;case3:printf("Nicework!\n");break;case4:printf("Keepupthegoodwork!\n");break;default:printf("Wrongtype!");}}/*函數(shù)功能:生成一個題目做錯的隨機提示*/voidPrintRandomWrongEvaluation(){inti;i=rand()%4+1;switch(i){case1:printf("No.Pleasetryagain.\n");break;case2:printf("Wrong.Tryoncemore.\n");break;case3:printf("Don'tgiveup!\n");break;case4:printf("Notcorrect.Keeptrying.\n");break;default:printf("Wrongtype!");}}2、代碼如下:#include<stdio.h>#defineMAX2147483647longre(longint);intnonres(longints);intmain(){longintn,m;intcount=0;printf("Pleaseenetranumberoptionaly:");scanf("%ld",&n);printf("Thegenerationprocessofpalindrome:\n");while(!nonres((m=re(n))+n)){if(m+n>=MAX){printf("inputerror,break.\n");break;}else{printf("[%d]:%ld+%ld=%ld\n",++count,n,m,m+n);n+=m;}}printf("[%d]:%ld+%ld=%ld\n",++count,n,m,m+n);printf("Herewereachedtheaimatlast!\n");return0;}longre(longinta){longintt;for(t=0;a>0;a/=10)t=t*10+a%10;returnt;}intnonres(longints){if(re(s)==s){return1;}else{return0;}}習題7一、單項選擇題1、D 2、B 、A 4、C 5、C 6B二、填空題1、p<6,k=p2、&a[i],i%10==03、&a[i],&x,i--,x==a[i]4、&a[i],SIZE-1,a[i]=a[j],a[j]=t,j—5、c,s[j],j++三、閱讀程序,寫出運行結(jié)果1、678552、sum=63、Thearrayhasbeenreverted.2,4,5,7,6,0,11,9,7,3四、編程題1、代碼如下:#include<stdio.h>intcmp(inta[],intb[],charc){inti,j,aa1[10]={0},bb1[10]={0},aa2[10]={0},bb2[10]={0};charc1='a'+'b'-c;for(i=0;i<10;i++)for(j=0;j<10;j++)if(a[i]>b[j]){aa1[i]++;bb2[j]++;}elseif(a[i]<b[j]){aa2[i]++;bb1[j]++;}printf("%cbai:du\n",c);for(i=0;i<10;i++)printf("大于zhi%c數(shù)組:%d\t等于%c數(shù)組:%d\t小于%c數(shù)組:%d\n",c1,aa1[i],c1,10-aa1[i]-aa2[i],c1,aa2[i]);printf("%c數(shù)組:\n",c1);for(i=0;i<10;i++)printf("大于%c 數(shù)組:%d\t 等于%c 數(shù)組:%d\t 小于%c 數(shù)組:%d\n",c,bb1[i],c,10-bb1[i]-bb2[i],c,bb2[i]);for(i=j=0;i<10;i++)j+=aa1[i]-bb1[i];if(j==0)return 0;return }intmain(){inti,a[10],b[10],k;for(i=0;i<10;i++)scanf("%d",&a[i]);for(i=0;i<10;i++)scanf("%d",&b[i]);k=cmp(a,b,'a');if(k==0)printf("兩個數(shù)組并列\(zhòng)n");elseif(k==1)printf("ab\n");elseprintf("b數(shù)比a數(shù)大\n");return 0;}2、代碼如下:#include<stdio.h>voidinv(intx[],intn);intmain(){inti,a[10];for(i=0;i<10;i++)scanf("%d",&a[i]);inv(a,10);printf("Thearrayhasbeenreverted.\n");for(i=0;i<10;i++)printf("\n");return0;}voidinv(intx[],intn){intt,i,j,m=(n-1)/2;for(i=0;i<=m;i++){j=n-1-i;t=x[i];x[i]=x[j];x[j]=t;}}3、代碼如下:#include<stdio.h>#include<math.h>#defineN300doubleaver(doublex[],intn){doubleaver=0.0,sum=0.0;inti;for(i=0;i<n;i++)sum=sum+x[i];aver=sum/n;returnaver;}doubleaversum(doublex[],intn){doubleave,sum=0.0;inti;ave=aver(x,N);for(i=0;i<n;i++)if(ave<x[i])sum=sum+x[i];returnsum;}intmain(){doublex[N],sum=0.0,ave=0.0;inti;for(i=0;i<N;i++)x[i]=10*tan(3.0+i*i);ave=aver(x,N);sum=aversum(x,N);printf("aver=%f\nm=%f\n",ave,sum);return0;}4、首先從數(shù)學上分析該問題:假設(shè)第一行中的素數(shù)為n[1]、n[2]、n[3]….n[i]、…第二行中的差值為m[1]、m[2]、m[3]…m[j]…。其中m[j]為:m[j]=n[j+1]-n[j]。N=(n[2]-n[1])+(n[3]-n[2])+(n[4]-n[3])+…+(n[j+1]-n[j])=n[j+1]-n[1]199318981898。2代碼如下:#include"stdio.h"#include"stdlib.h"intmain(){inta[1994],i,j,num=0;//a0即可for(i=0;i<1994;i++)a[i]=0;for(i=3;i<=1993;i++){intkey=1;//key=1是表示是素數(shù)for(j=2;j<=i/2;j++)if(i%j==0){key=0;break;}if(key)a[i]=i;}for(i=3;i<1994;i++)//被減數(shù)一定大于1898+3{for(j=1898+3;j<1994;j++)if(a[j]-a[i]==1898){printf("%d%d\n",a[i],a[j]);num++;}}printf("\nd\n",num);system("pause");}5、代碼如下:參考教材,略。6、代碼如下:參考教材,略。本章實驗題1.學生成績管理系統(tǒng)V1.0略。習題8一、單項選擇題1、C 2、B 、B 4、B 5、D二、填空題1、int*pa,pa=&a,*pa,&a,pa2、*pb,*pa,*pb,&c,&b三、閱讀程序,寫出運行結(jié)果1、10,20,302、first:a=3,b=2second:a=3,b=2third:a=2,b=33、10,20,70,104、82四、編程題1、代碼如下:#include<stdio.h>voidSplitTime(longinttotal_sec,int*hr,int*min,int*sec);intmain(){longinttotal_sec;inthr,min,sec;printf("Entertotal_second:");scanf("%ld",&total_sec);SplitTime(total_sec,&hr,&min,&sec);printf("hr=%d,min=%d,sec=%d\n",hr,min,sec);system("pause");return0;}voidSplitTime(longinttotal_sec,int*hr,int*min,int*sec){inti,j;i=total_sec/60;j=total_sec-i*60;*sec=j;if(i<60){*min=i;*hr=0;}else{intk;*hr=i/60;k=i-*hr*60;*min=k;}}2、代碼如下:#include<stdio.h>voidSwap(int*p1,int*p2);intmain(){intx,y;printf("inputtwointegernumbers:");scanf("%d%d",&x,&y);Swap(&x,&y);printf("%d\t%d\n",x,y);return0;}voidSwap(int*p1,int*p2){inttemp;temp=*p1;*p1=*p2;*p2=temp;}本章實驗題1.學生成績管理系統(tǒng)V2.0略。一、單項選擇題
習題91、D 2、A 、D 4、C 5、D 6D 、C 8A 9、C二、閱讀程序,寫運結(jié)果1、XYZdefgh82、mo3、a=Iamateacher.a=Youareastudent.4、Iamateacher.三、編程題1、代碼如下:#include<stdio.h>#include<string.h>intinsert(char*s1,char*s2,intp){char*cp,*tcp;tcp=s1+strlen(s1);cp=s1+p;if(tcp<cp)return-1;while(tcp>=cp){*(tcp+strlen(s2))=*tcp;tcp--;}while(*s2!='\0'){*cp=*s2;cp++;s2++;}return0;}intmain(void){charstr1[128];charstr2[128];intf;gets(str1);gets(str2);scanf("%d",&f);insert(str1,str2,f);puts(str1);return0;}2、代碼如下:#include<stdio.h>#include<string.h>intstrcomp(char*s,char*t){while(*s!='\0'&&*t!='\0'){if(*s-*t>0)return1;if(*s-*t<0)return-1;s++;t++;}if(*s-*t>0)return1;if(*s-*t<0)return-1;return0;}intmain(void){charstr1[128];charstr2[128];intf;gets(str1);gets(str2);f=strcmp(str1,str2);printf("%d",f);return0;}3、代碼如下:#include<stdio.h>#include<string.h>voiddel(char*str,char*sav,intk,intm);intmain(){intm,n;chars1[80],s2[80];gets(s1);scanf("%d%d",&m,&n);puts(s2);return0;}voiddel(char*str,char*sav,intk,intm){inti,j=0;//j要初始化for(i=0;i<strlen(str);i++){if(i==k-1){i+=m;//跳過m個字符}sav[j++]=str[i];}sav[j]='\0';//這里是sav不是str}4、代碼如下:#include<stdio.h>#include<string.h>voidsort(char*str){inti,j,length=strlen(str);chartemp;for(i=0;i<length-1;i++) //if(*(str+i)>*(str+j)){}intmain(){
temp=*(str+i);*(str+i)=*(str+j);*(str+j)=temp;}charstr[256];gets(str);sort(str);puts(str);return0;}5、代碼如下:#include<stdio.h>#include<string.h>intmain(){charstr[256];longn=0;inti,m;gets(str);while(strcmp("stop",str)!=0){m=strlen(str);for(i=0;i<m;i++){if(str[i]!=''&&str[i+1]=='')n++;if(str[i]!=''&&str[i+1]=='\0')n++;if(str[i]==''&&str[i+1]=='')continue;if(str[i]==''&&str[i+1]!='')continue;}gets(str);}printf("n=%ld\n",n);return0;}6、代碼如下:#include<stdio.h>#include<string.h>intmain(){chars1[80],s2[40],*p1=s1,*p2=s2;printf("Input2string:\n");gets(s1);gets(s2);while(*p1!='\0') //s1while(*p2!='\0') //s2*p1++=*p2++;*p1='\0';while(p1!=s1) //{if(*p1!=32){p1--;continue;}else{p2=p1;while(p2!=s1) //{*p2=*(p2-1);p2--;}*p2='';p1--;}}puts(s1);return0;7、代碼如下:#include<stdio.h>#include<string.h>inta[27];intmain(){inti=0;chars[80],*p=s;printf("Inputastring:\n");gets(s);while(*p!='\0'){if(*p>='A'&&*p<='Z'){*p=*p+32;}if(*p>='a'&&*p<='z'){a[*p-'a']++;}elsea[26]++;p++;}for(i=0;i<26;i++)printf("%c=%d",i+'a',a[i]);printf("other=%d\n",a[26]);return0;}本章實驗題1V3.0略。一、單項選擇題
習題101、D 2、D 、D 4、D 5、C 6B 、D 8D二、填空題1、r=r+b[k]、*x=r2、main()、char *pstr[3]、*(patr+j)3、p++、w[i-1]三、編程題1、代碼如下:#include<stdio.h>#include<math.h>#defineN100doubleavernum(double*x,doublen,doubleave){doubleaver=0.0,a=0;inti;if(ave==0){for(i=0;i<n;i++)aver=aver+*(x+i);aver=aver/n;returnaver;}else{for(i=0;i<n;i++)if(ave<*(x+i))a++;returna;}}intmain(){doublex[N],m,aver;inti;for(i=0;i<N;i++)x[i]=200*cos(i*0.875);aver=avernum(x,N,0);m=avernum(x,N,aver);printf("aver=%f\nm=%f\n",aver,m);return0;}2、代碼如下:#include<stdio.h>#include<math.h>#defineN300doubleavermax(double*x,doublen,doubleave){doubleaver=0.0,a=0;inti;if(ave==0){i=0;while(i<n){aver=aver+*(x+i);i++;}aver=aver/n;returnaver;}else{i=0;while(i<n){if(ave<*(x+i))a+=*(x+i);i++;}returna;}}intmain(){doublex[N],m,aver;inti;while(i<N){x[i]=10*cos(3.0+i*i)/5;i++;}aver=avermax(x,N,0);m=avermax(x,N,aver);printf("aver=%f\nm=%f\n",aver,m);return0;}3、代碼如下:#include<stdio.h>#include<stdlib.h>#include<string.h>#defineN15inta[N][4];intb[N];char*name[]={" ","andwolf","andgoat","andcabbage"};intsearch(intStep){inti;/*若該種步驟能使各值均為1,則輸出結(jié)果,進入回歸步驟*/if(a[Step][0]+a[Step][1]+a[Step][2]+a[Step][3]==4){for(i=0;i<=Step;i++) /**/{printf("east:");if(a[i][0]==0)printf("wolf ");if(a[i][1]==0)printf("goat ");if(a[i][2]==0)printf("cabbage ");if(a[i][3]==0)printf("farmer ");if(a[i][0]&&a[i][1]&&a[i][2]&&a[i][3])printf("none");printf(" ");printf("west:");if(a[i][0]==1)printf("wolf ");if(a[i][1]==1)printf("goat ");if(a[i][2]==1)printf("cabbage ");if(a[i][3]==1)printf("farmer ");if(!(a[i][0]||a[i][1]||a[i][2]||a[i][3]))printf("none");printf("\n\n\n");if(i<Step)printf(" the%dtime\n",i+1);if(i>0&&i<Step){if(a[i][3]0) /**/{printf(" -----> farmer");printf("%s\n",name[b[i]+1]);}else /**/{printf(" <----- farmer");printf("%s\n",name[b[i]+1]);}}}printf("\n\n\n\n");return0;}for(i=0;i<Step;i++){if(memcmp(a[i],a[Step],160) */{return0;}}/**/if(a[Step][1]!=a[Step][3](a[Step][2]==a[Step][1]||a[Step][0]==a[Step][1])){return0;}/*遞歸,從帶第一種動物開始依次向下循環(huán),同時限定遞歸的界限*/for(i=-1;i<=2;i++){b[Step]=i;memcpy(a[Step+1],a[Step],16); /*制上步態(tài)進下步移*/a[Step+1][3]=1-a[Step+1][3]; /*農(nóng)去或回*/if(i==-1){search(Step+1); /**/}elseif(a[Step][i]a[Step][3]) */{a[Step+1][i]=a[Step+1][3]; 帶回物*/search(Step+1); /*行一*/}}return0;}intmain(){printf("\n\n :\n\n\n");search(0);return0;}4、代碼如下:#include<stdio.h>#include<math.h>
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 《社會心理因素》課件
- 《電信業(yè)風云》課件
- 寒假自習課 25春初中道德與法治八年級下冊教學課件 第二單元 第2課時 公民基本義務(wù)
- 《沙盤規(guī)則介紹》課件
- 《定價的基本策略》課件
- 班干部工作總結(jié)3篇
- 2023年學校志愿者心得體會字萬能-學校志愿者工作總結(jié)(5篇)
- 2023-2024年項目部安全培訓考試題附答案(典型題)
- 畢業(yè)銷售實習報告模板匯編八篇
- 2023年項目部安全管理人員安全培訓考試題及參考答案(模擬題)
- 企業(yè)法律顧問詳細流程
- 中國商貿(mào)文化商道
- 云數(shù)據(jù)中心建設(shè)項目可行性研究報告
- 《新生兒視網(wǎng)膜動靜脈管徑比的形態(tài)學分析及相關(guān)性研究》
- 無重大疾病隱瞞保證書
- 2024年春概率論與數(shù)理統(tǒng)計學習通超星期末考試答案章節(jié)答案2024年
- 企業(yè)形象設(shè)計(CIS)戰(zhàn)略策劃及實施計劃書
- 2023-2024學年廣西桂林市高二(上)期末數(shù)學試卷(含答案)
- xx公路與天然氣管道交叉方案安全專項評價報告
- 國家職業(yè)技術(shù)技能標準 6-31-01-09 工程機械維修工(堆場作業(yè)機械維修工)人社廳發(fā)202226號
- DB11∕T 1077-2020 建筑垃圾運輸車輛標識、監(jiān)控和密閉技術(shù)要求
評論
0/150
提交評論