C语言设计精髓OJ.docx
《C语言设计精髓OJ.docx》由会员分享,可在线阅读,更多相关《C语言设计精髓OJ.docx(36页珍藏版)》请在冰豆网上搜索。
C语言设计精髓OJ
2011/1字符串翻转
#include
#defineMAX80
voidinverse(charstr[]);
intmain()
{
inti;
charstr[MAX];
printf("Input:
\n");
gets(str);
inverse(str);
printf("Afterinverse:
\n%s\n",str);
return0;
}
voidinverse(charstr[])
chars[MAX];
inti,j,n;
n=strlen(str);
for(i=0,j=n-1;str[i]!
='\0';i++,j--)
s[j]=str[i];
s[n]='\0';
strcpy(str,s);
2011/2辗转相除求最大公约数习题7.7
intgcd(inta,intb);
inta,b;
printf("Inputab\n");
scanf("%d%d",&a,&b);
printf("Result:
\n%d",gcd(a,b));
intgcd(inta,intb)
intr;
if(b==0)
returna;
else
r=a%b;
a=b;
b=r;
returngcd(a,b);
12/1百万富翁计划
longa=0,i;
doubleb=0.0;
for(i=0;i<30;i++)
a+=100000;
b+=pow(2,i);
b=b/100.0;
printf("Rich:
%ld\nPoor:
%lf\n",a,b);
12/2**矩阵乘积(不会写)
#defineMAX10
inta,b,i,j,x,y,flag;
intA[MAX][MAX],B[MAX][MAX],C[MAX][MAX]={0};
for(i=0;i{for(j=0;j{scanf("%d",&A[i][j]);}}for(i=0;i{for(j=0;j{scanf("%d",&B[i][j]);}}for(i=0;i{flag=0;for(j=0;j{if(flag=0){x=i;y=j;flag=1;}C[x][y]+=(A[i][j]*B[j][i]);}}for(i=0;i{for(j=0;j{printf("%d",C[i][j]);}printf("\n");}return0;}2012/3字符串连接#include#includevoidMystrcat(chardstStr[],charsrcStr[]);#defineMAX20intmain(){chardstStr[MAX],srcStr[MAX];printf("Inputdst&&src:\n");gets(dstStr);gets(srcStr);Mystrcat(dstStr,srcStr);printf("After:%s\n",dstStr);return0;}voidMystrcat(chardstStr[],charsrcStr[]){//chars[MAX];inti,j,n;n=strlen(dstStr);for(i=n,j=0;srcStr[j]!='\0';i++,j++){dstStr[i]=srcStr[j];}dstStr[i]='\0';}13/1猴子吃桃#includeintmain(){inti,sum=1;for(i=1;i<=9;i++){sum=2*(sum+1);}printf("%d",sum);}13/2feedback数组#includeintMedian(intanswer[],intn);voidDataSort(inta[],intn);intmain(){intfeedback[20],n,i,median;printf("Inputtotalnumberiffeedback\n");scanf("%d",&n);printf("Inputfeedbacks\n");for(i=0;i{scanf("%d",&feedback[i]);}DataSort(feedback,n);median=Median(feedback,n);printf("Medianvalue=%d\n",median);return0;}voidDataSort(inta[],intn){inti,j,term;for(i=0;i{for(j=i+1;j{if(a[i]{term=a[i];a[i]=a[j];a[j]=term;}}}}intMedian(intanswer[],intn){if(n%2==1)returnanswer[n/2];if(n%2==0)return(answer[n/2]+answer[n/2-1])/2;}2013/3国名排序第十章例题10.4#include#include#defineMAX_LEN20#defineN10voidSortString(charstr[][MAX_LEN],intn);intmain(){inti;charstr[N][MAX_LEN];for(i=0;i{gets(str[i]);}SortString(str,N);printf("Sortedresults\n");for(i=0;i{printf("%s\n",str[i]);}return0;}voidSortString(charstr[][MAX_LEN],intn){inti,j;charterm[MAX_LEN];for(i=0;i{for(j=i+1;j{if(strcmp(str[i],str[j])>0){strcpy(term,str[i]);strcpy(str[i],str[j]);strcpy(str[j],term);}}}}/*SouthKoreaFinlandUnitedStatesEnglandAustraliaBrazilEgyptChinaMexicoSpain*/2014/1杨辉三角#includevoidYHTriangle(inta[][15],intn);voidPrintYHTriangle(inta[][15],intn);intmain(){intn;inta[15][15]={0};printf("Inputn(n<=15):\n");scanf("%d",&n);YHTriangle(a,n);PrintYHTriangle(a,n);return0;}voidYHTriangle(inta[][15],intn){inti,j;for(i=0;i<15;i++){for(j=0;j<15;j++){if(j==0||i==j){a[i][j]=1;}}}for(i=1;i<15;i++){for(j=1;j{a[i][j]=(a[i-1][j-1]+a[i-1][j]);}}}voidPrintYHTriangle(inta[][15],intn){inti,j;for(i=0;i{for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
for(j=0;j
scanf("%d",&A[i][j]);
for(i=0;i
for(j=0;j{scanf("%d",&B[i][j]);}}for(i=0;i{flag=0;for(j=0;j{if(flag=0){x=i;y=j;flag=1;}C[x][y]+=(A[i][j]*B[j][i]);}}for(i=0;i{for(j=0;j{printf("%d",C[i][j]);}printf("\n");}return0;}2012/3字符串连接#include#includevoidMystrcat(chardstStr[],charsrcStr[]);#defineMAX20intmain(){chardstStr[MAX],srcStr[MAX];printf("Inputdst&&src:\n");gets(dstStr);gets(srcStr);Mystrcat(dstStr,srcStr);printf("After:%s\n",dstStr);return0;}voidMystrcat(chardstStr[],charsrcStr[]){//chars[MAX];inti,j,n;n=strlen(dstStr);for(i=n,j=0;srcStr[j]!='\0';i++,j++){dstStr[i]=srcStr[j];}dstStr[i]='\0';}13/1猴子吃桃#includeintmain(){inti,sum=1;for(i=1;i<=9;i++){sum=2*(sum+1);}printf("%d",sum);}13/2feedback数组#includeintMedian(intanswer[],intn);voidDataSort(inta[],intn);intmain(){intfeedback[20],n,i,median;printf("Inputtotalnumberiffeedback\n");scanf("%d",&n);printf("Inputfeedbacks\n");for(i=0;i{scanf("%d",&feedback[i]);}DataSort(feedback,n);median=Median(feedback,n);printf("Medianvalue=%d\n",median);return0;}voidDataSort(inta[],intn){inti,j,term;for(i=0;i{for(j=i+1;j{if(a[i]{term=a[i];a[i]=a[j];a[j]=term;}}}}intMedian(intanswer[],intn){if(n%2==1)returnanswer[n/2];if(n%2==0)return(answer[n/2]+answer[n/2-1])/2;}2013/3国名排序第十章例题10.4#include#include#defineMAX_LEN20#defineN10voidSortString(charstr[][MAX_LEN],intn);intmain(){inti;charstr[N][MAX_LEN];for(i=0;i{gets(str[i]);}SortString(str,N);printf("Sortedresults\n");for(i=0;i{printf("%s\n",str[i]);}return0;}voidSortString(charstr[][MAX_LEN],intn){inti,j;charterm[MAX_LEN];for(i=0;i{for(j=i+1;j{if(strcmp(str[i],str[j])>0){strcpy(term,str[i]);strcpy(str[i],str[j]);strcpy(str[j],term);}}}}/*SouthKoreaFinlandUnitedStatesEnglandAustraliaBrazilEgyptChinaMexicoSpain*/2014/1杨辉三角#includevoidYHTriangle(inta[][15],intn);voidPrintYHTriangle(inta[][15],intn);intmain(){intn;inta[15][15]={0};printf("Inputn(n<=15):\n");scanf("%d",&n);YHTriangle(a,n);PrintYHTriangle(a,n);return0;}voidYHTriangle(inta[][15],intn){inti,j;for(i=0;i<15;i++){for(j=0;j<15;j++){if(j==0||i==j){a[i][j]=1;}}}for(i=1;i<15;i++){for(j=1;j{a[i][j]=(a[i-1][j-1]+a[i-1][j]);}}}voidPrintYHTriangle(inta[][15],intn){inti,j;for(i=0;i{for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
scanf("%d",&B[i][j]);
for(i=0;i{flag=0;for(j=0;j{if(flag=0){x=i;y=j;flag=1;}C[x][y]+=(A[i][j]*B[j][i]);}}for(i=0;i{for(j=0;j{printf("%d",C[i][j]);}printf("\n");}return0;}2012/3字符串连接#include#includevoidMystrcat(chardstStr[],charsrcStr[]);#defineMAX20intmain(){chardstStr[MAX],srcStr[MAX];printf("Inputdst&&src:\n");gets(dstStr);gets(srcStr);Mystrcat(dstStr,srcStr);printf("After:%s\n",dstStr);return0;}voidMystrcat(chardstStr[],charsrcStr[]){//chars[MAX];inti,j,n;n=strlen(dstStr);for(i=n,j=0;srcStr[j]!='\0';i++,j++){dstStr[i]=srcStr[j];}dstStr[i]='\0';}13/1猴子吃桃#includeintmain(){inti,sum=1;for(i=1;i<=9;i++){sum=2*(sum+1);}printf("%d",sum);}13/2feedback数组#includeintMedian(intanswer[],intn);voidDataSort(inta[],intn);intmain(){intfeedback[20],n,i,median;printf("Inputtotalnumberiffeedback\n");scanf("%d",&n);printf("Inputfeedbacks\n");for(i=0;i{scanf("%d",&feedback[i]);}DataSort(feedback,n);median=Median(feedback,n);printf("Medianvalue=%d\n",median);return0;}voidDataSort(inta[],intn){inti,j,term;for(i=0;i{for(j=i+1;j{if(a[i]{term=a[i];a[i]=a[j];a[j]=term;}}}}intMedian(intanswer[],intn){if(n%2==1)returnanswer[n/2];if(n%2==0)return(answer[n/2]+answer[n/2-1])/2;}2013/3国名排序第十章例题10.4#include#include#defineMAX_LEN20#defineN10voidSortString(charstr[][MAX_LEN],intn);intmain(){inti;charstr[N][MAX_LEN];for(i=0;i{gets(str[i]);}SortString(str,N);printf("Sortedresults\n");for(i=0;i{printf("%s\n",str[i]);}return0;}voidSortString(charstr[][MAX_LEN],intn){inti,j;charterm[MAX_LEN];for(i=0;i{for(j=i+1;j{if(strcmp(str[i],str[j])>0){strcpy(term,str[i]);strcpy(str[i],str[j]);strcpy(str[j],term);}}}}/*SouthKoreaFinlandUnitedStatesEnglandAustraliaBrazilEgyptChinaMexicoSpain*/2014/1杨辉三角#includevoidYHTriangle(inta[][15],intn);voidPrintYHTriangle(inta[][15],intn);intmain(){intn;inta[15][15]={0};printf("Inputn(n<=15):\n");scanf("%d",&n);YHTriangle(a,n);PrintYHTriangle(a,n);return0;}voidYHTriangle(inta[][15],intn){inti,j;for(i=0;i<15;i++){for(j=0;j<15;j++){if(j==0||i==j){a[i][j]=1;}}}for(i=1;i<15;i++){for(j=1;j{a[i][j]=(a[i-1][j-1]+a[i-1][j]);}}}voidPrintYHTriangle(inta[][15],intn){inti,j;for(i=0;i{for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
flag=0;
if(flag=0)
x=i;
y=j;
flag=1;
C[x][y]+=(A[i][j]*B[j][i]);
for(i=0;i{for(j=0;j{printf("%d",C[i][j]);}printf("\n");}return0;}2012/3字符串连接#include#includevoidMystrcat(chardstStr[],charsrcStr[]);#defineMAX20intmain(){chardstStr[MAX],srcStr[MAX];printf("Inputdst&&src:\n");gets(dstStr);gets(srcStr);Mystrcat(dstStr,srcStr);printf("After:%s\n",dstStr);return0;}voidMystrcat(chardstStr[],charsrcStr[]){//chars[MAX];inti,j,n;n=strlen(dstStr);for(i=n,j=0;srcStr[j]!='\0';i++,j++){dstStr[i]=srcStr[j];}dstStr[i]='\0';}13/1猴子吃桃#includeintmain(){inti,sum=1;for(i=1;i<=9;i++){sum=2*(sum+1);}printf("%d",sum);}13/2feedback数组#includeintMedian(intanswer[],intn);voidDataSort(inta[],intn);intmain(){intfeedback[20],n,i,median;printf("Inputtotalnumberiffeedback\n");scanf("%d",&n);printf("Inputfeedbacks\n");for(i=0;i{scanf("%d",&feedback[i]);}DataSort(feedback,n);median=Median(feedback,n);printf("Medianvalue=%d\n",median);return0;}voidDataSort(inta[],intn){inti,j,term;for(i=0;i{for(j=i+1;j{if(a[i]{term=a[i];a[i]=a[j];a[j]=term;}}}}intMedian(intanswer[],intn){if(n%2==1)returnanswer[n/2];if(n%2==0)return(answer[n/2]+answer[n/2-1])/2;}2013/3国名排序第十章例题10.4#include#include#defineMAX_LEN20#defineN10voidSortString(charstr[][MAX_LEN],intn);intmain(){inti;charstr[N][MAX_LEN];for(i=0;i{gets(str[i]);}SortString(str,N);printf("Sortedresults\n");for(i=0;i{printf("%s\n",str[i]);}return0;}voidSortString(charstr[][MAX_LEN],intn){inti,j;charterm[MAX_LEN];for(i=0;i{for(j=i+1;j{if(strcmp(str[i],str[j])>0){strcpy(term,str[i]);strcpy(str[i],str[j]);strcpy(str[j],term);}}}}/*SouthKoreaFinlandUnitedStatesEnglandAustraliaBrazilEgyptChinaMexicoSpain*/2014/1杨辉三角#includevoidYHTriangle(inta[][15],intn);voidPrintYHTriangle(inta[][15],intn);intmain(){intn;inta[15][15]={0};printf("Inputn(n<=15):\n");scanf("%d",&n);YHTriangle(a,n);PrintYHTriangle(a,n);return0;}voidYHTriangle(inta[][15],intn){inti,j;for(i=0;i<15;i++){for(j=0;j<15;j++){if(j==0||i==j){a[i][j]=1;}}}for(i=1;i<15;i++){for(j=1;j{a[i][j]=(a[i-1][j-1]+a[i-1][j]);}}}voidPrintYHTriangle(inta[][15],intn){inti,j;for(i=0;i{for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
for(j=0;j{printf("%d",C[i][j]);}printf("\n");}return0;}2012/3字符串连接#include#includevoidMystrcat(chardstStr[],charsrcStr[]);#defineMAX20intmain(){chardstStr[MAX],srcStr[MAX];printf("Inputdst&&src:\n");gets(dstStr);gets(srcStr);Mystrcat(dstStr,srcStr);printf("After:%s\n",dstStr);return0;}voidMystrcat(chardstStr[],charsrcStr[]){//chars[MAX];inti,j,n;n=strlen(dstStr);for(i=n,j=0;srcStr[j]!='\0';i++,j++){dstStr[i]=srcStr[j];}dstStr[i]='\0';}13/1猴子吃桃#includeintmain(){inti,sum=1;for(i=1;i<=9;i++){sum=2*(sum+1);}printf("%d",sum);}13/2feedback数组#includeintMedian(intanswer[],intn);voidDataSort(inta[],intn);intmain(){intfeedback[20],n,i,median;printf("Inputtotalnumberiffeedback\n");scanf("%d",&n);printf("Inputfeedbacks\n");for(i=0;i{scanf("%d",&feedback[i]);}DataSort(feedback,n);median=Median(feedback,n);printf("Medianvalue=%d\n",median);return0;}voidDataSort(inta[],intn){inti,j,term;for(i=0;i{for(j=i+1;j{if(a[i]{term=a[i];a[i]=a[j];a[j]=term;}}}}intMedian(intanswer[],intn){if(n%2==1)returnanswer[n/2];if(n%2==0)return(answer[n/2]+answer[n/2-1])/2;}2013/3国名排序第十章例题10.4#include#include#defineMAX_LEN20#defineN10voidSortString(charstr[][MAX_LEN],intn);intmain(){inti;charstr[N][MAX_LEN];for(i=0;i{gets(str[i]);}SortString(str,N);printf("Sortedresults\n");for(i=0;i{printf("%s\n",str[i]);}return0;}voidSortString(charstr[][MAX_LEN],intn){inti,j;charterm[MAX_LEN];for(i=0;i{for(j=i+1;j{if(strcmp(str[i],str[j])>0){strcpy(term,str[i]);strcpy(str[i],str[j]);strcpy(str[j],term);}}}}/*SouthKoreaFinlandUnitedStatesEnglandAustraliaBrazilEgyptChinaMexicoSpain*/2014/1杨辉三角#includevoidYHTriangle(inta[][15],intn);voidPrintYHTriangle(inta[][15],intn);intmain(){intn;inta[15][15]={0};printf("Inputn(n<=15):\n");scanf("%d",&n);YHTriangle(a,n);PrintYHTriangle(a,n);return0;}voidYHTriangle(inta[][15],intn){inti,j;for(i=0;i<15;i++){for(j=0;j<15;j++){if(j==0||i==j){a[i][j]=1;}}}for(i=1;i<15;i++){for(j=1;j{a[i][j]=(a[i-1][j-1]+a[i-1][j]);}}}voidPrintYHTriangle(inta[][15],intn){inti,j;for(i=0;i{for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
printf("%d",C[i][j]);
printf("\n");
2012/3字符串连接
voidMystrcat(chardstStr[],charsrcStr[]);
#defineMAX20
chardstStr[MAX],srcStr[MAX];
printf("Inputdst&&src:
gets(dstStr);
gets(srcStr);
Mystrcat(dstStr,srcStr);
printf("After:
%s\n",dstStr);
voidMystrcat(chardstStr[],charsrcStr[])
//chars[MAX];
n=strlen(dstStr);
for(i=n,j=0;srcStr[j]!
='\0';i++,j++)
dstStr[i]=srcStr[j];
dstStr[i]='\0';
13/1猴子吃桃
inti,sum=1;
for(i=1;i<=9;i++)
sum=2*(sum+1);
printf("%d",sum);
13/2feedback数组
intMedian(intanswer[],intn);
voidDataSort(inta[],intn);
intfeedback[20],n,i,median;
printf("Inputtotalnumberiffeedback\n");
scanf("%d",&n);
printf("Inputfeedbacks\n");
for(i=0;i{scanf("%d",&feedback[i]);}DataSort(feedback,n);median=Median(feedback,n);printf("Medianvalue=%d\n",median);return0;}voidDataSort(inta[],intn){inti,j,term;for(i=0;i{for(j=i+1;j{if(a[i]{term=a[i];a[i]=a[j];a[j]=term;}}}}intMedian(intanswer[],intn){if(n%2==1)returnanswer[n/2];if(n%2==0)return(answer[n/2]+answer[n/2-1])/2;}2013/3国名排序第十章例题10.4#include#include#defineMAX_LEN20#defineN10voidSortString(charstr[][MAX_LEN],intn);intmain(){inti;charstr[N][MAX_LEN];for(i=0;i{gets(str[i]);}SortString(str,N);printf("Sortedresults\n");for(i=0;i{printf("%s\n",str[i]);}return0;}voidSortString(charstr[][MAX_LEN],intn){inti,j;charterm[MAX_LEN];for(i=0;i{for(j=i+1;j{if(strcmp(str[i],str[j])>0){strcpy(term,str[i]);strcpy(str[i],str[j]);strcpy(str[j],term);}}}}/*SouthKoreaFinlandUnitedStatesEnglandAustraliaBrazilEgyptChinaMexicoSpain*/2014/1杨辉三角#includevoidYHTriangle(inta[][15],intn);voidPrintYHTriangle(inta[][15],intn);intmain(){intn;inta[15][15]={0};printf("Inputn(n<=15):\n");scanf("%d",&n);YHTriangle(a,n);PrintYHTriangle(a,n);return0;}voidYHTriangle(inta[][15],intn){inti,j;for(i=0;i<15;i++){for(j=0;j<15;j++){if(j==0||i==j){a[i][j]=1;}}}for(i=1;i<15;i++){for(j=1;j{a[i][j]=(a[i-1][j-1]+a[i-1][j]);}}}voidPrintYHTriangle(inta[][15],intn){inti,j;for(i=0;i{for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
scanf("%d",&feedback[i]);
DataSort(feedback,n);
median=Median(feedback,n);
printf("Medianvalue=%d\n",median);
voidDataSort(inta[],intn)
inti,j,term;
for(i=0;i{for(j=i+1;j{if(a[i]{term=a[i];a[i]=a[j];a[j]=term;}}}}intMedian(intanswer[],intn){if(n%2==1)returnanswer[n/2];if(n%2==0)return(answer[n/2]+answer[n/2-1])/2;}2013/3国名排序第十章例题10.4#include#include#defineMAX_LEN20#defineN10voidSortString(charstr[][MAX_LEN],intn);intmain(){inti;charstr[N][MAX_LEN];for(i=0;i{gets(str[i]);}SortString(str,N);printf("Sortedresults\n");for(i=0;i{printf("%s\n",str[i]);}return0;}voidSortString(charstr[][MAX_LEN],intn){inti,j;charterm[MAX_LEN];for(i=0;i{for(j=i+1;j{if(strcmp(str[i],str[j])>0){strcpy(term,str[i]);strcpy(str[i],str[j]);strcpy(str[j],term);}}}}/*SouthKoreaFinlandUnitedStatesEnglandAustraliaBrazilEgyptChinaMexicoSpain*/2014/1杨辉三角#includevoidYHTriangle(inta[][15],intn);voidPrintYHTriangle(inta[][15],intn);intmain(){intn;inta[15][15]={0};printf("Inputn(n<=15):\n");scanf("%d",&n);YHTriangle(a,n);PrintYHTriangle(a,n);return0;}voidYHTriangle(inta[][15],intn){inti,j;for(i=0;i<15;i++){for(j=0;j<15;j++){if(j==0||i==j){a[i][j]=1;}}}for(i=1;i<15;i++){for(j=1;j{a[i][j]=(a[i-1][j-1]+a[i-1][j]);}}}voidPrintYHTriangle(inta[][15],intn){inti,j;for(i=0;i{for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
for(j=i+1;j{if(a[i]{term=a[i];a[i]=a[j];a[j]=term;}}}}intMedian(intanswer[],intn){if(n%2==1)returnanswer[n/2];if(n%2==0)return(answer[n/2]+answer[n/2-1])/2;}2013/3国名排序第十章例题10.4#include#include#defineMAX_LEN20#defineN10voidSortString(charstr[][MAX_LEN],intn);intmain(){inti;charstr[N][MAX_LEN];for(i=0;i{gets(str[i]);}SortString(str,N);printf("Sortedresults\n");for(i=0;i{printf("%s\n",str[i]);}return0;}voidSortString(charstr[][MAX_LEN],intn){inti,j;charterm[MAX_LEN];for(i=0;i{for(j=i+1;j{if(strcmp(str[i],str[j])>0){strcpy(term,str[i]);strcpy(str[i],str[j]);strcpy(str[j],term);}}}}/*SouthKoreaFinlandUnitedStatesEnglandAustraliaBrazilEgyptChinaMexicoSpain*/2014/1杨辉三角#includevoidYHTriangle(inta[][15],intn);voidPrintYHTriangle(inta[][15],intn);intmain(){intn;inta[15][15]={0};printf("Inputn(n<=15):\n");scanf("%d",&n);YHTriangle(a,n);PrintYHTriangle(a,n);return0;}voidYHTriangle(inta[][15],intn){inti,j;for(i=0;i<15;i++){for(j=0;j<15;j++){if(j==0||i==j){a[i][j]=1;}}}for(i=1;i<15;i++){for(j=1;j{a[i][j]=(a[i-1][j-1]+a[i-1][j]);}}}voidPrintYHTriangle(inta[][15],intn){inti,j;for(i=0;i{for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
if(a[i]{term=a[i];a[i]=a[j];a[j]=term;}}}}intMedian(intanswer[],intn){if(n%2==1)returnanswer[n/2];if(n%2==0)return(answer[n/2]+answer[n/2-1])/2;}2013/3国名排序第十章例题10.4#include#include#defineMAX_LEN20#defineN10voidSortString(charstr[][MAX_LEN],intn);intmain(){inti;charstr[N][MAX_LEN];for(i=0;i{gets(str[i]);}SortString(str,N);printf("Sortedresults\n");for(i=0;i{printf("%s\n",str[i]);}return0;}voidSortString(charstr[][MAX_LEN],intn){inti,j;charterm[MAX_LEN];for(i=0;i{for(j=i+1;j{if(strcmp(str[i],str[j])>0){strcpy(term,str[i]);strcpy(str[i],str[j]);strcpy(str[j],term);}}}}/*SouthKoreaFinlandUnitedStatesEnglandAustraliaBrazilEgyptChinaMexicoSpain*/2014/1杨辉三角#includevoidYHTriangle(inta[][15],intn);voidPrintYHTriangle(inta[][15],intn);intmain(){intn;inta[15][15]={0};printf("Inputn(n<=15):\n");scanf("%d",&n);YHTriangle(a,n);PrintYHTriangle(a,n);return0;}voidYHTriangle(inta[][15],intn){inti,j;for(i=0;i<15;i++){for(j=0;j<15;j++){if(j==0||i==j){a[i][j]=1;}}}for(i=1;i<15;i++){for(j=1;j{a[i][j]=(a[i-1][j-1]+a[i-1][j]);}}}voidPrintYHTriangle(inta[][15],intn){inti,j;for(i=0;i{for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
term=a[i];
a[i]=a[j];
a[j]=term;
intMedian(intanswer[],intn)
if(n%2==1)
returnanswer[n/2];
if(n%2==0)
return(answer[n/2]+answer[n/2-1])/2;
2013/3国名排序第十章例题10.4
#defineMAX_LEN20
#defineN10
voidSortString(charstr[][MAX_LEN],intn);
charstr[N][MAX_LEN];
for(i=0;i{gets(str[i]);}SortString(str,N);printf("Sortedresults\n");for(i=0;i{printf("%s\n",str[i]);}return0;}voidSortString(charstr[][MAX_LEN],intn){inti,j;charterm[MAX_LEN];for(i=0;i{for(j=i+1;j{if(strcmp(str[i],str[j])>0){strcpy(term,str[i]);strcpy(str[i],str[j]);strcpy(str[j],term);}}}}/*SouthKoreaFinlandUnitedStatesEnglandAustraliaBrazilEgyptChinaMexicoSpain*/2014/1杨辉三角#includevoidYHTriangle(inta[][15],intn);voidPrintYHTriangle(inta[][15],intn);intmain(){intn;inta[15][15]={0};printf("Inputn(n<=15):\n");scanf("%d",&n);YHTriangle(a,n);PrintYHTriangle(a,n);return0;}voidYHTriangle(inta[][15],intn){inti,j;for(i=0;i<15;i++){for(j=0;j<15;j++){if(j==0||i==j){a[i][j]=1;}}}for(i=1;i<15;i++){for(j=1;j{a[i][j]=(a[i-1][j-1]+a[i-1][j]);}}}voidPrintYHTriangle(inta[][15],intn){inti,j;for(i=0;i{for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
gets(str[i]);
SortString(str,N);
printf("Sortedresults\n");
for(i=0;i{printf("%s\n",str[i]);}return0;}voidSortString(charstr[][MAX_LEN],intn){inti,j;charterm[MAX_LEN];for(i=0;i{for(j=i+1;j{if(strcmp(str[i],str[j])>0){strcpy(term,str[i]);strcpy(str[i],str[j]);strcpy(str[j],term);}}}}/*SouthKoreaFinlandUnitedStatesEnglandAustraliaBrazilEgyptChinaMexicoSpain*/2014/1杨辉三角#includevoidYHTriangle(inta[][15],intn);voidPrintYHTriangle(inta[][15],intn);intmain(){intn;inta[15][15]={0};printf("Inputn(n<=15):\n");scanf("%d",&n);YHTriangle(a,n);PrintYHTriangle(a,n);return0;}voidYHTriangle(inta[][15],intn){inti,j;for(i=0;i<15;i++){for(j=0;j<15;j++){if(j==0||i==j){a[i][j]=1;}}}for(i=1;i<15;i++){for(j=1;j{a[i][j]=(a[i-1][j-1]+a[i-1][j]);}}}voidPrintYHTriangle(inta[][15],intn){inti,j;for(i=0;i{for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
printf("%s\n",str[i]);
voidSortString(charstr[][MAX_LEN],intn)
inti,j;
charterm[MAX_LEN];
for(i=0;i{for(j=i+1;j{if(strcmp(str[i],str[j])>0){strcpy(term,str[i]);strcpy(str[i],str[j]);strcpy(str[j],term);}}}}/*SouthKoreaFinlandUnitedStatesEnglandAustraliaBrazilEgyptChinaMexicoSpain*/2014/1杨辉三角#includevoidYHTriangle(inta[][15],intn);voidPrintYHTriangle(inta[][15],intn);intmain(){intn;inta[15][15]={0};printf("Inputn(n<=15):\n");scanf("%d",&n);YHTriangle(a,n);PrintYHTriangle(a,n);return0;}voidYHTriangle(inta[][15],intn){inti,j;for(i=0;i<15;i++){for(j=0;j<15;j++){if(j==0||i==j){a[i][j]=1;}}}for(i=1;i<15;i++){for(j=1;j{a[i][j]=(a[i-1][j-1]+a[i-1][j]);}}}voidPrintYHTriangle(inta[][15],intn){inti,j;for(i=0;i{for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
for(j=i+1;j{if(strcmp(str[i],str[j])>0){strcpy(term,str[i]);strcpy(str[i],str[j]);strcpy(str[j],term);}}}}/*SouthKoreaFinlandUnitedStatesEnglandAustraliaBrazilEgyptChinaMexicoSpain*/2014/1杨辉三角#includevoidYHTriangle(inta[][15],intn);voidPrintYHTriangle(inta[][15],intn);intmain(){intn;inta[15][15]={0};printf("Inputn(n<=15):\n");scanf("%d",&n);YHTriangle(a,n);PrintYHTriangle(a,n);return0;}voidYHTriangle(inta[][15],intn){inti,j;for(i=0;i<15;i++){for(j=0;j<15;j++){if(j==0||i==j){a[i][j]=1;}}}for(i=1;i<15;i++){for(j=1;j{a[i][j]=(a[i-1][j-1]+a[i-1][j]);}}}voidPrintYHTriangle(inta[][15],intn){inti,j;for(i=0;i{for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
if(strcmp(str[i],str[j])>0)
strcpy(term,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],term);
/*
SouthKorea
Finland
UnitedStates
England
Australia
Brazil
Egypt
China
Mexico
Spain
*/
2014/1杨辉三角
voidYHTriangle(inta[][15],intn);
voidPrintYHTriangle(inta[][15],intn);
intn;
inta[15][15]={0};
printf("Inputn(n<=15):
YHTriangle(a,n);
PrintYHTriangle(a,n);
voidYHTriangle(inta[][15],intn)
for(i=0;i<15;i++)
for(j=0;j<15;j++)
if(j==0||i==j)
a[i][j]=1;
for(i=1;i<15;i++)
for(j=1;j
a[i][j]=(a[i-1][j-1]+a[i-1][j]);
voidPrintYHTriangle(inta[][15],intn)
for(i=0;i{for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
for(j=0;j{if(a[i][j]!=0){printf("%5d",a[i][j]);}}printf("\n");}}2014/2**国王的许诺第六章实验题#include#includeintmain(){doublesum=0;doublevolum;inti;for(i=0;i<64;i++){sum+=pow(2,i);}volum=sum/(1.42*1e8);printf("sum=%e\n",sum);printf("volum=%e\n",volum);return0;}2015/1计算n以内含6的所有自然数的倒数之和#includeintmain(){intn,i,flag,num;floatsum=0;printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
if(a[i][j]!
=0)
printf("%5d",a[i][j]);
2014/2**国王的许诺第六章实验题
doublesum=0;
doublevolum;
for(i=0;i<64;i++)
sum+=pow(2,i);
volum=sum/(1.42*1e8);
printf("sum=%e\n",sum);
printf("volum=%e\n",volum);
2015/1计算n以内含6的所有自然数的倒数之和
intn,i,flag,num;
floatsum=0;
printf("Inputn(0scanf("%d",&n);for(i=1;i<=n;i++){num=i;flag=0;while(num!=0){if(num%10==6)flag=1;num/=10;}if(flag==1){sum+=1.0/i;}}printf("%.2f\n",sum);return0;}2015/2字符串每个字符之间插入一个空格#include#include#defineMAX80voidInsert(chars[]);intmain(){chars[MAX];inti;printf("Inputastring:\n");gets(s);Insert(s);printf("Insertresult:\n");printf("%s\n",s);return0;}voidInsert(chars[]){charstr[MAX];inti,j,len;len=strlen(s);for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
for(i=1;i<=n;i++)
num=i;
while(num!
if(num%10==6)
num/=10;
if(flag==1)
sum+=1.0/i;
printf("%.2f\n",sum);
2015/2字符串每个字符之间插入一个空格
voidInsert(chars[]);
printf("Inputastring:
gets(s);
Insert(s);
printf("Insertresult:
printf("%s\n",s);
voidInsert(chars[])
inti,j,len;
len=strlen(s);
for(i=0,j=0;i{str[j]=s[i];j++;str[j]='';j++;}str[j]='\0';strcpy(s,str);s[j]='\0';}2015/3评委打分#include#defineM5voidReadScore(floatscore[][M],intn,intm);voidCountScore(floataveragescore[],floatscore[][M],intn,intm);voidPrintScore(floataveragescore[],intn);intmain(){floatscore[20][M]={0.0};floataveragescore[20]={0.0};intn,m;printf("Inputthenumberofathletes:\n");scanf("%d",&n);printf("Inputthenumberofjudges:\n");scanf("%d",&m);ReadScore(score,n,m);CountScore(averagescore,score,n,m);PrintScore(averagescore,n);return0;}voidReadScore(floatscore[][M],intn,intm){inti,j;printf("Input%djudges'scoresfor%dathletes\n",m,n);for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
str[j]=s[i];
j++;
str[j]='';
str[j]='\0';
strcpy(s,str);
s[j]='\0';
2015/3评委打分
#defineM5
voidReadScore(floatscore[][M],intn,intm);
voidCountScore(floataveragescore[],floatscore[][M],intn,intm);
voidPrintScore(floataveragescore[],intn);
floatscore[20][M]={0.0};
floataveragescore[20]={0.0};
intn,m;
printf("Inputthenumberofathletes:
printf("Inputthenumberofjudges:
scanf("%d",&m);
ReadScore(score,n,m);
CountScore(averagescore,score,n,m);
PrintScore(averagescore,n);
voidReadScore(floatscore[][M],intn,intm)
printf("Input%djudges'scoresfor%dathletes\n",m,n);
for(i=0;i{for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
for(j=0;j{scanf("%f",&score[i][j]);}}}voidCountScore(floataveragescore[],floatscore[][M],intn,intm){intmaxn,minn,i,j;for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
scanf("%f",&score[i][j]);
voidCountScore(floataveragescore[],floatscore[][M],intn,intm)
intmaxn,minn,i,j;
for(i=0;i{maxn=0,minn=0;for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
maxn=0,minn=0;
for(j=0;j{if(score[i][j]>score[i][maxn]){maxn=j;}if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
if(score[i][j]>score[i][maxn])
maxn=j;
if(score[i][j]{minn=j;}}for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
minn=j;
for(j=0;j{if(j!=maxn&&j!=minn){averagescore[i]+=score[i][j];}}averagescore[i]/=(m-2);}}voidPrintScore(floataveragescore[],intn){inti;printf("Thefinalscoresforeachathlete\n");for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
if(j!
=maxn&&j!
=minn)
averagescore[i]+=score[i][j];
averagescore[i]/=(m-2);
voidPrintScore(floataveragescore[],intn)
printf("Thefinalscoresforeachathlete\n");
for(i=0;i{printf("%.2f\n",averagescore[i]);}}习题5.8#include#includeintmain(){charc;printf("Input:\n");//scanf("%c",&c);c=getchar();if(isspace(c)){printf("Space!\n");}elseif(isdigit(c)){printf("Digit!\n");}elseif(c>='a'&&c<='z'){printf("Schar!\n");}elseif(c>='A'&&c<='Z'){printf("Bchar!\n");}return0;}习题5.10#includeintmain(){intyear,mon;printf("Inputy/m\n");scanf("%d%d",&year,&mon);switch(mon){case1:case3:case5:case7:case8:case10:case12:printf("31days\n");break;case2:if(year%4==0&&year%100!=0||year%400==0)printf("29days\n");elseprintf("28days\n");break;case4:case6:case9:case11:printf("30days\n");break;default:printf("Inputerror!\n");return-1;}}例题6.4#include#includeintmain(){intn,i;intpf=1,lf=1;printf("Inputn:\n");scanf("%d",&n);for(i=1;i<=n;i++){printf("%d:pf:%dlf:%d\n",i,i*i,i*i*i);}return0;}6.3改错题1.main可以不写返回类型,不算错2.=-》==3.x++在if外执行习题6.6教育资金问题**注意:要迭代五次而不是四次#includeintmain(){floatx=0.0,y;inti;for(i=0;i<5;i++){y=(x+1000.0)/(1+0.01875*12);x=y;}printf("Result:%f\n",y);return0;}6.7工业产值翻翻#includeintmain(){intyear,i=0;floatrate;longbj=1e6,zj;printf("Inputrate:\n");scanf("%f",&rate);while(bj<2e6){bj*=(1+rate);i++;}printf("After%dyears!\n",i);return0;}6.8pi值的计算**应该声明为double而非float#include#include#defineN1e-4intmain(){doublepi,n,sum=0.0,fz=-1,fm;inti=1;do{fz*=-1;fm=2*i-1;n=fz/fm;sum+=n;i++;}while(fabs(n)>=N);printf("pi:%lfi:%d\n",4*sum,i-1);return0;}6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;#include#include#defineN1e-5intmain(){doublesum=1.0,n=1.0;inti=1;do{n*=i;sum+=(1/n);i++;}while(fabs(1/n)>=1e-5);printf("e:%lfi:%d\n",sum,i);return0;}6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu#includeintmain(){unsignedlongn,i=1,jc=1,sum=0;printf("Inputn:\n");scanf("%lu",&n);do{jc*=i;sum+=jc;i++;}while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
printf("%.2f\n",averagescore[i]);
习题5.8
charc;
//scanf("%c",&c);
c=getchar();
if(isspace(c))
printf("Space!
elseif(isdigit(c))
printf("Digit!
elseif(c>='a'&&c<='z')
printf("Schar!
elseif(c>='A'&&c<='Z')
printf("Bchar!
习题5.10
intyear,mon;
printf("Inputy/m\n");
scanf("%d%d",&year,&mon);
switch(mon)
case1:
case3:
case5:
case7:
case8:
case10:
case12:
printf("31days\n");
break;
case2:
if(year%4==0&&year%100!
=0||year%400==0)
printf("29days\n");
printf("28days\n");
case4:
case6:
case9:
case11:
printf("30days\n");
default:
printf("Inputerror!
return-1;
例题6.4
intn,i;
intpf=1,lf=1;
printf("Inputn:
printf("%d:
pf:
%dlf:
%d\n",i,i*i,i*i*i);
6.3改错题
1.main可以不写返回类型,不算错
2.=-》==
3.x++在if外执行
习题6.6教育资金问题**注意:
要迭代五次而不是四次
floatx=0.0,y;
for(i=0;i<5;i++)
y=(x+1000.0)/(1+0.01875*12);
x=y;
%f\n",y);
6.7工业产值翻翻
intyear,i=0;
floatrate;
longbj=1e6,zj;
printf("Inputrate:
scanf("%f",&rate);
while(bj<2e6)
bj*=(1+rate);
i++;
printf("After%dyears!
\n",i);
6.8pi值的计算**应该声明为double而非float
#defineN1e-4
doublepi,n,sum=0.0,fz=-1,fm;
inti=1;
do
fz*=-1;
fm=2*i-1;
n=fz/fm;
sum+=n;
while(fabs(n)>=N);
printf("pi:
%lfi:
%d\n",4*sum,i-1);
6.9e值计算**尤其注意这一题,从1开始加的,后面才是通项,e是2.7几,count是10;
#defineN1e-5
doublesum=1.0,n=1.0;
n*=i;
sum+=(1/n);
while(fabs(1/n)>=1e-5);
printf("e:
%d\n",sum,i);
6.11计算阶乘和的上限**注意输入格式用unsignedlong输出格式用%lu
unsignedlongn,i=1,jc=1,sum=0;
scanf("%lu",&n);
jc*=i;
sum+=jc;
while(sumprintf("Result:%d",i-2);return0;}6.18九九乘法表(一)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(二)#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>=j)printf("%3d",i*j);}printf("\n");}return0;}6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格#includeintmain(){inti,j;for(i=1;i<=9;i++){for(j=1;j<=9;j++){if(i>j){printf("");}elseprintf("%3d",i*j);}printf("\n");}return0;}7.8水手分椰子问题****极易错答案3121#includeintCocount(intn);intmain(){printf("y=%d\n",Cocount(5));return0;}intCocount(intn){floatx=1,y;inti=1;y=n*x+1;do{y=y*n/(n-1)+1;if(y!=(int)y){x++;y=n*x+1;i=1;}i++;}while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
%d",i-2);
6.18九九乘法表
(一)
for(j=1;j<=9;j++)
printf("%3d",i*j);
(二)
if(i>=j)
6.18九九乘法表(三)**注意,与二不同,前面不显示的是空格
if(i>j)
printf("");
elseprintf("%3d",i*j);
7.8水手分椰子问题****极易错答案3121
intCocount(intn);
printf("y=%d\n",Cocount(5));
intCocount(intn)
floatx=1,y;
y=n*x+1;
y=y*n/(n-1)+1;
if(y!
=(int)y)
x++;
i=1;
while(ireturn(int)y;}习题7.12**汉诺塔问题,易错#includevoidHanoi(intn,chara,charb,charc);voidMove(intn,chara,charb);intmain(){intn;printf("Inputthenumofdisk:\n");scanf("%d",&n);Hanoi(n,'A','B','
return(int)y;
习题7.12**汉诺塔问题,易错
voidHanoi(intn,chara,charb,charc);
voidMove(intn,chara,charb);
printf("Inputthenumofdisk:
Hanoi(n,'A','B','
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1