《C语言程序设计》苏小红课后习题答案_第1页
《C语言程序设计》苏小红课后习题答案_第2页
《C语言程序设计》苏小红课后习题答案_第3页
《C语言程序设计》苏小红课后习题答案_第4页
《C语言程序设计》苏小红课后习题答案_第5页
已阅读5页,还剩93页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

2.2#include<stdio.h>main(){floatx=2.5,y=2.5,z=2.5;printf("x=%f\n",x);printf("y=%f\n",y);printf("z=%f\n",z);}3.1(1)#include<stdio.h>main(){inta=12,b=3;floatx=18.5,y=4.6;printf("%f\n",(float)(a*b)/2);printf("%d\n",(int)x%(int)y);}3.1(2)#include<stdio.h>main(){ intx=32,y=81,p,q; p=x++; q=--y; printf("%d%d\n",p,q); printf("%d%d\n",x,y);}3.2#include<stdio.h>main(){intx,b0,b1,b2,s;printf("Inputx:");scanf("%d",&x);b2=x/100;b1=(x-b2*100)/10;//或(x%100)/10;或x/10%10;b0=x%10;s=b0*100+b1*10+b2;printf("s=%d\n",s);}3.3#include<stdio.h>#include<math.h>main(){floatrate=0.0225;floatn,capital,deposit;printf("Inputn,capital:");scanf("%f,%f",&n,&capital);deposit=capital*pow(1+rate,n);printf("deposit=%f\n",deposit);}3.4#include<stdio.h>#include<math.h>main(){floata,b,c;doublex,y;printf("Inputa,b,c:");scanf("%f%f%f",&a,&b,&c);x=(-b+sqrt(b*b-4*a*c))/(2*a);y=(-b-sqrt(b*b-4*a*c))/(2*a);printf("x=%f,y=%f\n",x,y);}习题44.1(1)#include<stdio.h>main(){charc1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3);}4.1(2)#include<stdio.h>main(){inta=12,b=15;printf("a=%d%%,b=%d%%\n",a,b);}4.1(3)#include<stdio.h>main(){inta,b;scanf("%2d%*2s%2d",&a,&b);printf("%d,%d\n",a,b);}4.2#include<stdio.h>main(){longa,b;floatx,y;scanf("%d,%d\n",&a,&b);scanf("%f,%f\n",&x,&y);printf("a=%d,b=%d\n",a,b);printf("x=%f,b=%f\n",x,y);}5.1#include<stdio.h>main(){floata;printf("Inputa:");scanf("%f",&a);if(a>=0){a=a;printf("a=%f\n",a);}else{a=-a;printf("a=%f\n",a);}}5.2#include<stdio.h>main(){inta;printf("Inputa:");scanf("%d",&a);if(a%2==0){printf("a是偶数");}else{printf("a是奇数");}}5.3#include<stdio.h>#include<math.h>main(){floata,b,c,s,area;printf("Inputa,b,c:");scanf("%f%f%f",&a,&b,&c);if(a+b>c&&a+c>b&&b+c>a){s=(a+b+c)/2;area=(float)sqrt(s*(s-a)*(s-b)*(s-c));printf("area=%f\n",area);}else{printf("不是三角形");}}5.4#include<stdio.h>#include<math.h>main(){floata,b,c,x,y;printf("Inputa,b,c:");scanf("%f,%f,%f",&a,&b,&c);if(a==0){printf("该方程不是一元二次方程\n");}if(b*b-4*a*c>0){x=(-b+sqrt(b*b-4*a*c))/(2*a);y=(-b-sqrt(b*b-4*a*c))/(2*a);printf("x=%f,y=%f\n",x,y);}elseif(b*b-4*a*c==0){x=-b/(2*a);y=-b/(2*a);printf("x=%f,y=%f\n",x,y);}else{printf("该方程无实根\n");}}5.5#include<stdio.h>main(){intprintf("Inputayear:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0){flag=1;}else{flag=0;}if(flag==1){printf("%daleapyear!\n",year);}else{printf("%dnotaleapyear!\n",year);}}5.6#include<stdio.h>main(){intprintf("Inputayear:");scanf("%d",&year);flag=year%400==0||year%4==0&&year%100!=0?1:0;if(flag==1&&flag!=0){printf("%daleapyear!\n",year);}else{printf("%dnotaleapyear!\n",year);}}5.7#include<stdio.h>main(){charch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}elseif(ch>='A'&&ch<='Z'){ch=getchar();ch=ch+32;printf("%c,%d\n",ch,ch);}else{printf("%c",ch);}}5.8#include<stdio.h>main(){charch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch是数字字符\n");}elseif(ch>=65&&ch<=90){printf("ch是大写字母\n");}elseif(ch>=97&&ch<=122){printf("ch是小写字母\n");}elseif(ch==32){printf("ch是空格\n");}else{printf("ch是其他字符\n");}}5.9#include<stdio.h>main(){intscore,grade;printf("Inputscore:");scanf("%d",&score);grade=score/10;if(score<0||score>100){printf("Inputerror\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}elseif(score>=80&&score<90){printf("%d--B\n",score);}elseif(score>=70&&score<80){printf("%d--C\n",score);}elseif(score>=60&&score<70){printf("%d--D\n",score);}elseif(score>=0&&score<60){printf("%d--E\n",score);}}5.10#include<stdio.h>main(){intprintf("Inputscanf("%d,%d",&year,&month);if(month>12||month<=0){printf("errormonth\n");}else{switch(year,month){case12:case10:case8:case7:case5:case3:case1:printf("31天\n");break;casecase9:case6:case4:printf("30天\n");break;case2:if(year%4==0&&year!=0||year%400==0){printf("29天\n");}else{printf("28天\n");}break;default:printf("Inputerror\n");}}}6.1(1)#include<stdio.h>main(){inti,j,k;charspace='for(i=1;i<=4;i++){for(j=1;j<=i;j++){printf("%c",space);}for(k=1;k<=6;k++){printf("*");}printf("\n");}}6.1(2)#include<stdio.h>main(){intk=4,n;for(n=0;n<k;n++){if(n%2==0)continue;k--;}printf("k=%d\n,n=%d\n",k,n);}6.1(3)#include<stdio.h>main(){intk=4,n;for(n=0;n<k;n++){if(n%2==0)break;k--;}printf("k=%d,n=%d\n",k,n);}6.2(1)#include<stdio.h>main(){inti,sum=0;for(i=1;i<=101;i++){sum=sum+i;}printf("sum=%d\n",sum);}6.2(2)#include<stdio.h>main(){longlongterm,sum=0;for(i=1;i<=101;i=i+2){term=i*(i+1)*(i+2);sum=sum+term;}printf("sum=%ld\n",sum);}6.2(4)#include<stdio.h>#include<math.h>main(){intn=1;floatterm=1.0,sign=1,sum=0;while(term<=-1e-4||term>=1e-4){term=1.0/sign;sum=sum+term;sign=sign+n;n++;}printf("sum=%f\n",sum);}6.2(5)#include<stdio.h>#include<math.h>main(){intn=1,count=1;floatx;doublesum,term;printf("Inputx:");scanf("%f",&x);sum=x;term=x;do{term=-term*x*x/((n+1)*(n+2));sum=sum+term;n=n+2;count++;}while(fabs(term)>=1e-5);printf("sin(x)=%f,count=%d\n",sum,count);}6.3#include<stdio.h>main(){intx=1,find=0;while(!find){if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0){printf("x=%d\n",x);find=1;x++;}}}intx,find=0;for(x=1;!find;x++){if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0){printf("x=%d\n",x);find=1;}}6.4#include<stdio.h>main(){inti,n;longp=1,m=1;printf("Inputn:");scanf("%d",&n);for(i=1;i<=n;i++){p=i*i;m=i*i*i;printf("p=%d,m=%d\n",i,p,i,m);}}6.5#include<stdio.h>main(){floatc,f;for(c=-40;c<=110;c=c+10){f=9/5*c+32;printf("f=%f\n",f);}}6.6#include<stdio.h>#include<math.h>main(){intn;doublec=0.01875,x;do{x=x*pow(1+c,12)-1000;n++;}while(x>0);printf("x=%d\n",x);}6.7#include<stdio.h>main(){intn=0;floata=100.0,c;printf("Inputc:");scanf("%f",&c);do{a=a*(1+c);n++;}while(a<=200);printf("n=%d\n",n);}6.8#include<stdio.h>#include<math.h>main(){intn=1,count=1;doublesum=1,term=1;while(fabs(term)>=1e-5){term=pow(-1,count)*(1.0/(n+2));sum=sum+term;n=n+2;count++;}sum=4*sum;printf("sum=%f,count=%d\n",sum,count);}6.9#include<stdio.h>#include<math.h>main(){intn=1,count=1;doublesum=1,term=1;while(fabs(term)>=1e-5){term=term*(1.0/n);sum=sum+term;n++;count++;}printf("sum=%f,count=%d\n",sum,count);}6.10#include<stdio.h>#include<math.h>main(){intx;for(x=100;x<=999;x++){if(x==pow(x/100,3)+pow(x/10%10,3)+pow(x%10,3))printf("x=%d\n",x);}}#include<stdio.h>main(){inti=0,n;longsum=0,term=1;printf("Inputn:");scanf("%d",&n);do{i++;term=term*i;sum=sum+term;}while(sum<n);printf("%d\n",i-1);}6.12#include<stdio.h>main(){inti,n,m,count=0,sum=0;printf("Inputn:");scanf("%d",&n);for(i=1;i<=n;i++){printf("Inputm:");scanf("%d",&m);if(m>0){sum=sum+m;count++;}else{break;}printf("sum=%d,count=%d\n",sum,count);}}6.13#include<stdio.h>main(){inti,n,m,count=0,sum=0;printf("Inputn:");scanf("%d",&n);for(i=1;i<=n;i++){printf("Inputm:");scanf("%d",&m);if(m>0||m<0){sum=sum+m;count++;}else{break;}printf("sum=%d,count=%d\n",sum,count);}}6.14#include<stdio.h>main(){intfor(x=0;x<=17;x++){for(y=0;y<=25;y++){3*x+2*y+z==50;z=30-x-y;if(3*x+2*y+z==50&&x+y+z==30)printf("x=%d,y=%d,z=%d\n",x,y,z);}}}6.15#include<stdio.h>main(){intx,y;for(x=0;x<=98;x++){y=98-x;2*x+4*y==386;if(x+y==98&&2*x+4*y==386){printf("x=%d,y=%d\n",x,y);}}}6.16#include<stdio.h>main(){intfor(x=0;x<=20;x++){for(y=0;y<=33;y++){3*y+5*x+z/3.0==100;z=100-x-y;(5*x+3*y+z/3.0==100&&z+x+y==100){printf("x=%d,y=%d,z=%d\n",x,y,z);}}}}6.17#include<stdio.h>main(){intfor(x=1;x<=9;x++){for(y=1;y<=17;y++){10*x+5*y+z==100;z=50-x-y;if(10*x+5*y+z==100&&x+y+z==50&&z>0){printf("x=%d,y=%d,z=%d\n",x,y,z);}}}}7.1#include<stdio.h>intSquare(int{returni*i;}intmain(){inti=0;i=Square(i);for(;i<3;i++){staticinti=1;i+=Square(i);printf("%d,",i);}printf("%d\n",i);return0;}7.2#include<stdio.h>inthour,minute,second;voidupdate(){second++;if(second==60){second=0;minute++;}if(minute==60){minute=0;hour++;}if(hour==24)hour=0;}voiddisplay(){printf("%d,%d,%d\n",hour,minute,second);}voiddelay(){intfor(t=0;t<100000000;t++);}intmain(){intvoidupdaye(),display(),delay();for(i=0;i<1000000;i++){update();display();delay();}return0;}7.3#include<stdio.h>intGetMax(inta,intb);intmain(){intprintf("Inputx,y:");scanf("%d,%d",&x,&y);max=GetMax(x,y);printf("max=%d\n",max);return0;}intGetMax(intm,intn){if(m>=n)returnm;elsereturnn;}7.4#include<stdio.h>intLCM(intn,intm);intmain(){inta,b;printf("Inputa,b:");scanf("%d,%d",&a,&b);printf("%d\n",LCM(a,b));return0;}intLCM(intn,intm){intx;intfind=0;for(x=1;!find;x++){if(x%n==0&&x%m==0){find=1;}}returnx-1;}7.5#include<stdio.h>longFact(intn);intmain(){intm,a;printf("Inputm:");scanf("%d",&m);for(a=1;a<=m;a++){printf("%d!=%ld\n",a,Fact(a));}return0;}longFact(intn){intlongresult=1;for(i=2;i<=n;i++)result*=i;returnresult;}7.6#include<stdio.h>longFact(intn);intmain(){intm;longret;printf("Inputm:");scanf("%d",&m);ret=Fact(m);printf("ret=%d\n",ret);return0;}longFact(intn){intlongresult=1,sum=0;for(i=2;i<=n;i++){result*=i;sum=sum+result;}returnsum;}7.7(1)#include<stdio.h>intGcd(inta,intb);intmain(){intm,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}intGcd(inta,intb){intt,min,find=0;min=a<b?a:b;t=min;for(t=min;!find;t--){a%t==0;b%t==0;if(a%t==0&&b%t==0)return}find=1;}7.7(2)#include<stdio.h>intGcd(inta,intb);intmain(){intm,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}intGcd(inta,intb){intr=a%b;if(r==0)returnb;elsedo{temp=b;b=r;a=temp;r=a%b;}while(r!=0);returnb;}8.1(1)#include<stdio.h>voidFunc(intx){x=2;}intmain(){intx=10;Func(x);printf("%d",x);return0;}8.1(2)#include<stdio.h>voidFunc(intb[]){intfor(j=0;j<4;j++){b[j]=j;}}intmain(){staticinta[]={5,6,7,8},i;Func(a);for(i=0;i<4;i++){printf("%d",a[i]);}return0;}8.2(1)intPositiveNum(inta[],intn){inti,count=0;for(i=0;i<n;i++){if(a[i]>0)count++;}return0;}8.2(2)voidFib(longf[],longn){inti;f[0]=0;f[1]=1;for(i=2;i<n;i++){f[i]=f[i-1]+f[i-2];}}8.2(3)#include<stdio.h>intmain(){inta[10],n,max,min,maxPos,minPos;for(n=0;n<10;n++){scanf("%d",&a[n]);}max=min=a[0];maxPos=minPos=0;for(n=0;n<10;n++){if(a[n]>max){max=a[n];maxPos=n;}elseif(a[n]<min){min=a[n];minPos=n;}}printf("max=%d,pos=%d\n",max,maxPos);printf("min=%d,pos=%d\n",min,minPos);return0;}8.3voidDivArray(intn){intfor(i=0;i<n;i++){pArray[i]/=pAttay[0];}}8.4#include<stdio.h>#defineN40intFailNum(intscore[],intn);voidReadScore(intscore[],intn);intmain(){intscore[N],n;printf("Inputn:");scanf("%d",&n);ReadScore(score,n);FailNum(score,n);printf("FailNumstudentsare%d\n",FailNum(score,n));return0;}intFailNum(intscore[],intn){inti,count=0;for(i=0;i<n;i++){if(score[i]<60)count++;}returncount;}voidReadScore(intscore[],intn){intprintf("Inputscore:");for(i=0;i<n;i++){scanf("%d",&score[i]);}}8.5#include<stdio.h>#defineN40intscore[],intn);intReadScore(intscore[]);intmain(){intscore[N],n;n=ReadScore(score);studentsarereturn0;}intscore[],intn){inti,count=0,sum=0,aver;for(i=0;i<n;i++){sum=sum+score[i];aver=sum/n;}for(i=0;i<n;i++){if(score[i]>aver)count++;}returncount;}intReadScore(intscore[]){inti=-1;do{i++;printf("Inputscore:");scanf("%d",&score[i]);}while(score[i]>=0);return}8.6#include<stdio.h>#defineN40intReadScore(intscore[],longnum[]);intFindMax(intscore[],longnum[],intn);intmain(){intscore[N],n,max,num,t;longnum[N];t=FindMax(score,n);max=score[t];num=num[t];printf("max=%d,num=%d\n",max,num);return0;}intReadScore(intscore[],longnum[]){inti=-1;do{i++;printf("Inputstudent'sIDandscore:");scanf("%ld%d",&num[i],&score[i]);}while(num[i]>0&&score[i]>=0);return}intFindMax(intscore[],longnum[],intn){intmax,i;max=score[0];for(i=1;i<n;i++){if(score[i]>max)max=score[i];}return}8.7#include<stdio.h>#defineN40intRead(inta[]);intChangeArry(inta[],intn);intmain(){inta[N],n;printf("%d\n",ChangeArry(a,n));return0;}intRead(inta[]){intfor(i=0;i<9;i++){printf("Inputa:");scanf("%d",&a[i]);return}intChangeArry(inta[],intn){intmax,min,i,maxpos,minpos,term;max=min=score[0];for(i=1;i<n;i++){if(a[i]>max)max=score[i];maxpos=n;else(a[i]<min)min=a[i];minpos=n;temp=maxpos;maxpos=minpos;minpos=temp;}returna[i];}8.10#include<stdio.h>#defineN5voidmain(){inta[N][N];intsum=0;intprintf("请输入一个%d*%d的矩阵\n",N,N);for(i=0;i<N;i++){for(j=0;j<N;j++){scanf("%d",&a[i][j]);}}for(i=0;i<N;i++){for(j=0;j<N;j++){if(iji+jN-1)sum=sum+a[i][j];}}printf("对角线元素之和为:%d\n",sum);}#include<stdio.h>#defineN2#defineM3voidmain(){inta[N][M],b[N][M];inti,j,sum1=0,sum2=0,sum=0;printf("请输入一个%d*%d的矩阵\n",N,M);for(i=0;i++){for(j=0;{scanf("%d",&a[i][j]);sum1=sum1+a[i][j];}}printf("请输入一个%d*%d的矩阵\n",N,M);for(i=0;ii++){for(j=0;j{scanf("%d",&b[i][j]);sum2=sum2+a[i][j];}}sum=sum1+sum2;printf("sum=%d\n",sum);}8.12#include<stdio.h>#defineN7voida[N][N],intn);voidmain(){inti,j,n,b[N][N];printf("Inputn:");scanf("%d",&n);for(i=0;i<n;i++){for(j=0;j<=i;j++){printf("%4d",b[i][j]);}printf("\n");}}voida[N][N],intn){inti,j;for(i=0;i<n;i++){a[i][0]=1;a[i][i]=1;}for(i=2;i<n;i++){for(j=1;j<i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];}}8.13#include<stdio.h>#defineN12voidmain(){intintf[N]={1,1};printf("%d:%d\n",0,f[0]);printf("%d:%d\n",1,f[1]);for(i=2;i<N;i++){f[i]=f[i-1]+f[i-2];printf("%d:%d\n",i+1,f[i]);}}8.14#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN6000voidmain(){inta[N],i;intb1=0,b2=0,b3=0,b4=0,b5=0,b6=0;floatp1,p2,p3,p4,p5,p6;srand(time(NULL));for(i=0;i<N;i++){a[i]=rand()%6+1;printf("%3d",a[i]);if(a[i]==1)b1++;if(a[i]==2)b2++;if(a[i]==3)b3++;if(a[i]==4)b4++;if(a[i]==5)b5++;if(a[i]==6)b6++;p1=(float)(b1/6000.0);p2=(float)(b2/6000.0);p3=(float)(b3/6000.0);p4=(float)(b4/6000.0);p5=(float)(b5/6000.0);p6=(float)(b6/6000.0);}printf("骰子出现1的概率:%.2f\n",p1);printf("骰子出现2的概率:%.2f\n",p2);printf("骰子出现3的概率:%.2f\n",p3);printf("骰子出现4的概率:%.2f\n",p4);printf("骰子出现5的概率:%.2f\n",p5);printf("骰子出现6的概率:%.2f\n",p6);}9.1(1)voidSwap(int*x,int{int未进行初始化,指向哪里位置*x=*y;}9.1(2)voidSwap(int*x,int{intx=y;}错误,x,y没有指向类型9.2#include<stdio.h>intmain(){inta[]={1,2,3,4,5};int*p=a;printf("%d,%d,%d,%d,%d,%d,%d\n",*p,*(++p),(*p)++,*p,*p--,--(*p),*p);return0;}9.3#include<stdio.h>voidSwap(int*x,int*y);intmain(){inta[6]={1,2,3,4,5,6},b[6]={11,22,33,44,55,66};intprintf("BeforeArray\n");for(i=0;i<6;i++){printf("%d\n",a[i]);}for(i=0;i<6;i++){printf("%d\n",b[i]);}for(i=0;i<6;i++){Swap(&a[i],&b[i]);}printf("AfterArray\n");for(i=0;i<6;i++){printf("%d\n",a[i]);}for(i=0;i<6;i++){printf("%d\n",b[i]);}return0;}voidSwap(int*x,int{inttemp;temp=*x;*x=*y;*y=temp;}9.4#include<stdio.h>#defineN10voidFindMaxMin(inta[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum);intmain(){inta[N];inti,maxa,mina,maxnum,minnum;printf("Inputa:");for(i=0;i<10;i++){scanf("%d",&a[i]);}FindMaxMin(a,&maxa,&maxnum,&mina,&minnum);printf("maxa=%d,maxnum=%d,mina=%d,minnum=%d\n",maxa,maxnum,mina,minnum);return0;}voidFindMaxMin(inta[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum){int*pMaxa=a[0];*pMina=a[0];*pMaxnum=0;*pMinnum=0;for(i=0;i<10;i++){if(a[i]>*pMaxa){*pMaxa=a[i];*pMaxnum=i;}elseif(a[i]<*pMina){*pMina=a[i];*pMinnum=i;}}}10.1voidMYStrcpy(charchar*srcStr){while((*dstStr++=*srcStr++)!='\0'){}}10.2#include<stdio.h>main(){char*a="main(){charc%s%c;printf(a,34,a,34);}";printf(a,34,a,34);}10.3(1)unsignedintMyStrlen(charmain(){unsignedintlen;len=0;for(;*pa!='\0';pa++){len++;}returnlen;}10.3(2)unsignedintMyStrlen(chars[]){charwhile(*p!='\0'){p++;}return(p-*p);}10.3(3)intMyStrcmp(char*p1,char*p2){for(;*p1==*p2;p1++,p2++){if(*p1=='\0')return-1;}return1;}10.3(4)intMyStrcmp(chars[],chart[]){intfor(i=0;s[i]==t[i];i++){if(s[i]=='\0')return

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论