C语言大学实用教程 课后参考答案_苏小红

合集下载

《C语言程序设计》(苏小红)课后习题答案高等教育出版社

《C语言程序设计》(苏小红)课后习题答案高等教育出版社
sca nf("%d", &year);
if(year%4==0&&year%400!=0||year%400==0)
{
flag=1;
lag==1)
餐4
P13
ttinc-udeAsfdio.hv
maino

charclHao2Hbo3H-c二
prinff(=a%cb%cc%2rTQo2o3)八
4.1(2)
ttinc-udeAsfdio.hv
maino
infaH_kzbH—k5八p「inff(=aH%d%%』bH%d%%一rrab)八
4三3)
/bbrjr)
if(a>=0)
{
a=a;
prin tf("a=%f\n",a);
}
else
{
a=-a;
prin tf("a=%f\n",a);
}
}
5.2
#i nclude<stdio.h>
mai n()
{
int a;
prin tf("I nput a:"); sca nf("%d",&a);
if(a%2==0)
}
float x=18.5,y=4.6;
prin tf("%f\n",(float)(a*b)/2); prin tf("%d\n",(i nt)x%(i nt)y); }
3.1(2)
#i nclude<stdio.h>
mai n()
{
int x=32,y=81,p,q;
p=x++;

C语言程序设计苏小红版答案解析

C语言程序设计苏小红版答案解析

完美 WORD 格式2.2 #include<stdio.h>main(){float x=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(){int a=12,b=3;float x=18.5,y=4.6;printf("%d\n",(float)(a*b)/2);printf("%d\n",(int)x%(int)y);}3.2 #include<stdio.h>main(){int x,b0,b1,b2,s;printf("Inputx:");scanf("%d",&x);b2=x/100;b1=(x-b2*100)/10;b0=x%10;s=b0*100+b1*10+b2;printf("s=%d\n",s);}3.3 #include<stdio.h>#include<math.h>main(){float rate=0.0225;float n,capital,deposit;printf("Input n,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(){float a,b,c;专业知识分享完美 WORD 格式double x,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);}4.1(1) #include<stdio.h>main(){char c1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3); }4.1(2) #include<stdio.h>main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b); }4.1(3) #include<stdio.h>main(){int a,b;scanf("%2d%*2s%2d",&a,&b);printf("%d,%d\n",a,b);}4.2 #include<stdio.h>main(){long a,b;float x,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(){float a;printf("Innputa:");scanf("%f",&a);if(a>=0){专业知识分享完美 WORD 格式a=a;printf("a=%f\n",a);}else{a=-a;printf("a=%f\n",a);}}5.2 #include<stdio.h>main(){int a;printf("Inputa:");scanf("%d",&a);if(a%2==0){printf("a 是偶数 ");}else{printf("a 是奇数 ");}}5.3 #include<stdio.h>#include<math.h>main(){float a,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>专业知识分享完美 WORD 格式main(){float a,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);}else if(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(){int year,flag;printf("Input a year:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0) {flag=1;}else{flag=0;}if(flag==1){printf("%d is a leap year !\n",year); }专业知识分享完美 WORD 格式else{printf("%d is not a leap year !\n",year);}}5.6 #include<stdio.h>main(){int year,flag;printf("Input a year:");scanf("%d",&year);flag=year%400==0||year%4==0&&year%100!=0?1:0;if(flag==1&&flag!=0){printf("%d is a leap year !\n",year);}else{printf("%d is not a leap year !\n",year);}}5.7 #include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}else if(ch>='A'&&ch<='Z'){ch=getchar();ch=ch+32;printf("%c,%d\n",ch,ch);}else{printf("%c",ch);}}专业知识分享完美 WORD 格式5.8 #include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch 是数字字符 \n");}else if(ch>=65&&ch<=90){printf("ch 是大写字母 \n");}else if(ch>=97&&ch<=122){printf("ch 是小写字母 \n");}else if(ch==32){printf("ch 是空格 \n");}else{printf("ch 是其他字符 \n");}}5.9 #include<stdio.h>main(){int score,grade;printf("Input score:");scanf("%d",&score);grade=score/10;if(score<0||score>100){printf("Input error\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90){专业知识分享完美 WORD 格式printf("%d--B\n",score);}else if(score>=70&&score<80){printf("%d--C\n",score);}else if(score>=60&&score<70){printf("%d--D\n",score);}else if(score>=0&&score<60){printf("%d- -E\n",score);}}5.10 #include<stdio.h>main(){int year,month;printf("Input year,month:");scanf("%d,%d",&year,&month);if(month>12||month<=0){printf("error month\n");}else{switch(year,month){case 12:case 10:case 8:case 7:case 5:case 3:case 1:printf("31 天 \n");break;case 11:case 9:case 6:case 4:printf("30 天 \n");break;专业知识分享完美 WORD 格式case 2:if(year%4==0&&year!=0||year%400==0){printf("29 天 \n");}else{printf("28 天 \n");}break;default:printf("Input error\n");}}}6.1(1) #include<stdio.h>main(){int i,j,k;char space=' ';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(){int k=4,n;for(n=0;n<k;n++){if(n%2==0) continue;k--;}printf("k=%d\n,n=%d\n",k,n);}专业知识分享完美 WORD 格式6.1(3) #include<stdio.h>main(){int k=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(){int i,sum=0;for(i=1;i<=101;i++){sum=sum+i;}printf("sum=%d\n",sum); }6.2(2) #include<stdio.h>main(){long i;long term,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(){int n=1;float term=1.0,sign=1,sum=0; while(term<=-1e-4||term>=1e-4) {term=1.0/sign;sum=sum+term;sign=sign+n;专业知识分享完美 WORD 格式n++;}printf("sum=%f\n",sum);}6.2(5) #include<stdio.h>#include<math.h>main(){int n=1,count=1;float x;double sum,term;printf("Input x:");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(){int x=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++;}}}/* int x,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;专业知识分享完美 WORD 格式}}}*/6.4 #include<stdio.h>main(){int i,n;long p=1,m=1;printf("Input n:");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(){float c,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(){int n;double c=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(){int n=0;专业知识分享完美 WORD 格式float a=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(){int n=1,count=1;double sum=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(){int n=1,count=1;double sum=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>专业知识分享完美 WORD 格式main(){int x;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);}}6.11 #include<stdio.h>main(){int i=0,n;long sum=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(){int i,n,m,count=0,sum=0;printf("Input n:");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);专业知识分享完美 WORD 格式}}6.13 #include<stdio.h>main(){int i,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(){int x,y,z;for(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(){int x,y;for(x=0;x<=98;x++)专业知识分享完美 WORD 格式{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(){int x,y,z;for(x=0;x<=20;x++){for(y=0;y<=33;y++){3*y+5*x+z/3.0==100;z=100-x-y;if (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(){int x,y,z;for(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);}}}专业知识分享完美 WORD 格式}7.1 #include<stdio.h> int Square(int i){return i*i;}int main(){int i=0;i=Square(i);for( ;i<3;i++){static int i=1;i+=Square(i);printf("%d,",i);}printf("%d\n",i);return 0;}7.2 #include<stdio.h> int hour,minute,second; void update(){second++;if(second==60){second=0;minute++;}if(minute==60){minute=0;hour++;}if(hour==24)hour=0;}void display(){printf("%d,%d,%d\n",hour,minute,second);}void delay(){专业知识分享完美 WORD 格式int t;for(t=0;t<100000000;t++);}int main(){int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){update();display();delay();}return 0;}7.3 #include<stdio.h>int GetMax(int a,int b);int main(){int x,y,max;printf("Inputx,y:");scanf("%d,%d",&x,&y);max=GetMax(x,y);printf("max=%d\n",max);return 0;}int GetMax(int m,int n){if(m>=n)return m;elsereturn n;}7.4 #include<stdio.h>int LCM(int n,int m);int main(){int a,b;printf("Inputa,b:");scanf("%d,%d",&a,&b);printf("%d\n",LCM(a,b));return 0;}int LCM(int n,int m)专业知识分享完美 WORD 格式{int x;int find=0;for(x=1;!find;x++){if(x%n==0&&x%m==0){find=1;}}return x-1;}7.5 #include<stdio.h>long Fact(int n);int main(){int m,a;printf("Inputm:");scanf("%d",&m);for(a=1;a<=m;a++){printf("%d!=%ld\n",a,Fact(a));}return 0;}long Fact(int n){int i;long result=1;for(i=2;i<=n;i++)result*=i;return result;}7.6 #include<stdio.h>long Fact(int n);int main(){int m;long ret;printf("Inputm:");scanf("%d",&m);ret=Fact(m);printf("ret=%d\n",ret);专业知识分享完美 WORD 格式return 0;}long Fact(int n){int i;long result=1,sum=0;for(i=2;i<=n;i++){result*=i;sum=sum+result;}return sum;}7.7(1) #include<stdio.h>int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return 0;}int Gcd(int a,int b){int t,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 t;}find=1;}7.7(2) #include<stdio.h>int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);专业知识分享完美 WORD 格式printf("%d\n",Gcd(m,n));return 0;}int Gcd(int a,int b){int r,temp;r=a%b;if(r==0)return b;elsedo{temp=b;b=r;a=temp;r=a%b;}while(r!=0);return b;}8.1(1) #include<stdio.h> void Func(int x){x=2;}int main(){int x=10;Func(x);printf("%d",x);return 0;}8.1(2) #include<stdio.h> void Func(int b[]){int j;for(j=0;j<4;j++){b[j]=j;}}int main(){static int a[]={5,6,7,8},i;Func(a);for(i=0;i<4;i++)专业知识分享完美 WORD 格式{printf("%d",a[i]);}return 0;}8.2(1) int PositiveNum(int a[],int n) {int i,count=0;for(i=0;i<n;i++){if(a[i]>0)count++;}return 0;}8.2(2) void Fib(long f[],long n){int i;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>int main(){int a[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;}else if(a[n]<min){min=a[n];专业知识分享完美 WORD 格式minPos=n;}}printf("max=%d,pos=%d\n",max,maxPos);printf("min=%d,pos=%d\n",min,minPos);return 0;}8.3 void DivArray(int *pArray,int n){int i;for(i=0;i<n;i++){pArray[i]/=pAttay[0];}}8.4 #include<stdio.h>#define N 40int FailNum(int score[],int n);void ReadScore(int score[],int n);int main(){int score[N],n;printf("Input n:");scanf("%d",&n);ReadScore(score,n);FailNum(score,n);printf("FailNum students are %d\n",FailNum(score,n));return 0;}int FailNum(int score[],int n){int i,count=0;for(i=0;i<n;i++){if(score[i]<60)count++;}return count;}void ReadScore(int score[],int n){int i;printf("Input score:");for(i=0;i<n;i++)专业知识分享完美 WORD 格式{scanf("%d",&score[i]);}}8.5 #include<stdio.h>#define N 40int HighAver(int score[],int n);int ReadScore(int score[]);int main(){int score[N],n;n=ReadScore(score);printf("HighAver students are %d\n",HighAver(score,n));return 0;}int HighAver(int score[],int n){int i,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++;}return count;}int ReadScore(int score[]){int i=-1;do{i++;printf("Input score:");scanf("%d",&score[i]);}while(score[i]>=0);return i;}8.6 #include<stdio.h>#define N 40int ReadScore(int score[],long num[]);int FindMax(int score[],long num[],int n);专业知识分享完美 WORD 格式int main(){int score[N],n,max,num,t;long num[N];t=FindMax(score,n);max=score[t];num=num[t];printf("max=%d,num=%d\n",max,num);return 0;}int ReadScore(int score[],long num[]){int i=-1;do{i++;printf("Input student's ID and score:");scanf("%ld%d",&num[i],&score[i]);}while(num[i]>0&&score[i]>=0);return i;}int FindMax(int score[],long num[],int n){int max,i;max=score[0];for(i=1;i<n;i++){if(score[i]>max)max=score[i];}return i;}8.7 #include<stdio.h>#define N 40int Read(int a[]);int ChangeArry(int a[],int n);int main(){int a[N],n;printf("%d\n",ChangeArry(a,n));return 0;}int Read(int a[]){int i;专业知识分享完美 WORD 格式for(i=0;i<9;i++){printf("Input a:");scanf("%d",&a[i]);return i;}int ChangeArry(int a[],int n){int max,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;}return a[i];}8.10 #include<stdio.h>#define N 5void main(){int a[N][N];int sum = 0;int i, j;printf(" 请输入一个 %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++){专业知识分享。

C语言程序设计苏小红版答案

C语言程序设计苏小红版答案

{float x=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(){int a=12,b=3;float x=18.5,y=4.6;printf("%d\n",(float)(a*b)/2);printf("%d\n",(int)x%(int)y); }3.2 #include<stdio.h>{int x,b0,b1,b2,s;printf("Inputx:");scanf("%d",&x);b2=x/100;b1=(x-b2*100)/10;b0=x%10;s=b0*100+b1*10+b2;printf("s=%d\n",s); }3.3 #include<stdio.h> #include<math.h>main(){float rate=0.0225;float n,capital,deposit;printf("Input n,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(){float a,b,c;double x,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);}4.1(1) #include<stdio.h>main(){char c1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3); }4.1(2) #include<stdio.h>main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b); }4.1(3) #include<stdio.h>main(){int a,b;scanf("%2d%*2s%2d",&a,&b);printf("%d,%d\n",a,b);}4.2 #include<stdio.h>main(){long a,b;float x,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(){float a;printf("Innputa:");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(){int a;printf("Inputa:");scanf("%d",&a);if(a%2==0){printf("a是偶数");}else{printf("a是奇数");}}5.3 #include<stdio.h>#include<math.h>main(){float a,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(){float a,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);}else if(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(){int year,flag;printf("Input a year:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0) {flag=1;}else{flag=0;}if(flag==1){printf("%d is a leap year !\n",year); }elseprintf("%d is not a leap year !\n",year);}}5.6 #include<stdio.h>main(){int year,flag;printf("Input a year:");scanf("%d",&year);flag=year%400==0||year%4==0&&year%100!=0?1:0;if(flag==1&&flag!=0){printf("%d is a leap year !\n",year);}elseprintf("%d is not a leap year !\n",year);}}5.7 #include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}else if(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(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch是数字字符\n");}else if(ch>=65&&ch<=90){printf("ch是大写字母\n");}else if(ch>=97&&ch<=122) {printf("ch是小写字母\n");}else if(ch==32){printf("ch是空格\n");}else{printf("ch是其他字符\n");}}5.9 #include<stdio.h>main(){int score,grade;printf("Input score:");scanf("%d",&score);grade=score/10;if(score<0||score>100){printf("Input error\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90) {printf("%d--B\n",score);}else if(score>=70&&score<80) {printf("%d--C\n",score);}else if(score>=60&&score<70) {printf("%d--D\n",score);else if(score>=0&&score<60){printf("%d- -E\n",score);}}5.10 #include<stdio.h>main(){int year,month;printf("Input year,month:");scanf("%d,%d",&year,&month);if(month>12||month<=0){printf("error month\n");}{switch(year,month){case 12:case 10:case 8:case 7:case 5:case 3:case 1:printf("31天\n");break;case 11:case 9:case 6:case 4:printf("30天\n");break;case 2:if(year%4==0&&year!=0||year%400==0){printf("29天\n");}else{printf("28天\n");}break;default:printf("Input error\n");}}}6.1(1) #include<stdio.h> main(){int i,j,k;char space=' ';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(){int k=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>{int k=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(){int i,sum=0;for(i=1;i<=101;i++){sum=sum+i;}printf("sum=%d\n",sum); }6.2(2) #include<stdio.h> main(){long i;long term,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(){int n=1;float term=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(){int n=1,count=1;float x;double sum,term;printf("Input x:");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(){int x=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++;}}}/* int x,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(){int i,n;long p=1,m=1;printf("Input n:");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(){float c,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(){int n;double c=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(){int n=0;float a=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(){double sum=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(){double sum=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(){int x;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);}}6.11 #include<stdio.h>main(){int i=0,n;long sum=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(){int i,n,m,count=0,sum=0;printf("Input n:");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(){int i,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(){int x,y,z;for(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(){int x,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(){int x,y,z;for(x=0;x<=20;x++){for(y=0;y<=33;y++){3*y+5*x+z/3.0==100;z=100-x-y;if (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(){int x,y,z;for(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>int Square(int i){return i*i;}int main(){int i=0;i=Square(i);for( ;i<3;i++){static int i=1;i+=Square(i);printf("%d,",i);}printf("%d\n",i);return 0;}7.2 #include<stdio.h> int hour,minute,second; void update(){second++;if(second==60){second=0;minute++;}if(minute==60){minute=0;hour++;}if(hour==24)hour=0;}void display(){printf("%d,%d,%d\n",hour,minute,second); }void delay(){int t;}int main(){int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){update();display();delay();}return 0;}7.3 #include<stdio.h>int GetMax(int a,int b); int main(){int x,y,max;printf("Inputx,y:");scanf("%d,%d",&x,&y);max=GetMax(x,y);printf("max=%d\n",max);return 0;}int GetMax(int m,int n) {if(m>=n)return m;elsereturn n;}7.4 #include<stdio.h> int LCM(int n,int m);int main(){int a,b;printf("Inputa,b:");scanf("%d,%d",&a,&b);printf("%d\n",LCM(a,b));return 0;}int LCM(int n,int m){int x;int find=0;for(x=1;!find;x++){if(x%n==0&&x%m==0){find=1;}}return x-1;}7.5 #include<stdio.h>long Fact(int n);int main(){int m,a;printf("Inputm:");scanf("%d",&m);for(a=1;a<=m;a++){printf("%d!=%ld\n",a,Fact(a));}return 0;}long Fact(int n){int i;long result=1;for(i=2;i<=n;i++)result*=i;return result;}7.6 #include<stdio.h>long Fact(int n);int main(){int m;long ret;printf("Inputm:");scanf("%d",&m);ret=Fact(m);printf("ret=%d\n",ret);return 0;}long Fact(int n){int i;long result=1,sum=0;for(i=2;i<=n;i++){result*=i;sum=sum+result;}return sum;}7.7(1) #include<stdio.h> int Gcd(int a,int b);int main(){。

C语言程序设计苏小红版答案

C语言程序设计苏小红版答案

#include<>main(){float x=,y=,z=;printf("x=%f\n",x);printf("y=%f\n",y);printf("z=%f\n",z);}(1) #include<>main(){int a=12,b=3;float x=,y=;printf("%d\n",(float)(a*b)/2);printf("%d\n",(int)x%(int)y); }#include<>main(){int x,b0,b1,b2,s;printf("Inputx:");scanf("%d",&x);b2=x/100;b1=(x-b2*100)/10;b0=x%10;s=b0*100+b1*10+b2;printf("s=%d\n",s);}#include<>#include<>main(){float rate=;float n,capital,deposit;printf("Input n,capital:");scanf("%f,%f",&n,&capital);deposit=capital*pow(1+rate,n);printf("deposit=%f\n",deposit); }#include<>#include<>main(){float a,b,c;double x,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);}(1) #include<>main(){char c1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3); }(2) #include<>main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b); }(3) #include<>main(){int a,b;scanf("%2d%*2s%2d",&a,&b);printf("%d,%d\n",a,b);}#include<>main(){long a,b;float x,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);}#include<>main(){float a;printf("Innputa:");scanf("%f",&a);if(a>=0){a=a;printf("a=%f\n",a);}else{a=-a;printf("a=%f\n",a);}}#include<>main(){int a;printf("Inputa:");scanf("%d",&a);if(a%2==0){printf("a是偶数");}else{printf("a是奇数");}}#include<>#include<>main(){float a,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("不是三角形");}}#include<>#include<>{float a,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);}else if(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");}}#include<>main(){int year,flag;printf("Input a year:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0){flag=1;}else{flag=0;}if(flag==1){printf("%d is a leap year !\n",year);}{printf("%d is not a leap year !\n",year);}}#include<>main(){int year,flag;printf("Input a year:");scanf("%d",&year);flag=year%400==0||year%4==0&&year%100!=0?1:0;if(flag==1&&flag!=0){printf("%d is a leap year !\n",year);}else{printf("%d is not a leap year !\n",year);}}#include<>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}else if(ch>='A'&&ch<='Z'){ch=getchar();ch=ch+32;printf("%c,%d\n",ch,ch);}else{printf("%c",ch);}}#include<>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch是数字字符\n");}else if(ch>=65&&ch<=90){printf("ch是大写字母\n");}else if(ch>=97&&ch<=122){printf("ch是小写字母\n");}else if(ch==32){printf("ch是空格\n");}else{printf("ch是其他字符\n");}}#include<>main(){int score,grade;printf("Input score:");scanf("%d",&score);grade=score/10;if(score<0||score>100){printf("Input error\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90){printf("%d--B\n",score);}else if(score>=70&&score<80) {printf("%d--C\n",score);}else if(score>=60&&score<70) {printf("%d--D\n",score);}else if(score>=0&&score<60){printf("%d- -E\n",score);}}#include<>main(){int year,month;printf("Input year,month:");scanf("%d,%d",&year,&month);if(month>12||month<=0){printf("error month\n");}else{switch(year,month){case 12:case 10:case 8:case 7:case 5:case 3:case 1:printf("31天\n");break;case 11:case 9:case 6:case 4:printf("30天\n");break;case 2:if(year%4==0&&year!=0||year%400==0){printf("29天\n");}else{printf("28天\n");}break;default:printf("Input error\n");}}}(1) #include<>main(){int i,j,k;char space=' ';for(i=1;i<=4;i++){for(j=1;j<=i;j++){printf("%c",space);}for(k=1;k<=6;k++){printf("*");}printf("\n");}}(2) #include<>main(){int k=4,n;for(n=0;n<k;n++){if(n%2==0) continue;k--;}printf("k=%d\n,n=%d\n",k,n);}(3) #include<>main(){int k=4,n;for(n=0;n<k;n++){if(n%2==0) break;k--;}printf("k=%d,n=%d\n",k,n);}(1) #include<>main(){int i,sum=0;for(i=1;i<=101;i++){sum=sum+i;}printf("sum=%d\n",sum);}(2) #include<>main(){long i;long term,sum=0;for(i=1;i<=101;i=i+2){term=i*(i+1)*(i+2);sum=sum+term;}printf("sum=%ld\n",sum);}(4) #include<>#include<>main(){int n=1;float term=,sign=1,sum=0;while(term<=-1e-4||term>=1e-4){term=sign;sum=sum+term;sign=sign+n;n++;}printf("sum=%f\n",sum);}(5) #include<>#include<>main(){int n=1,count=1;float x;double sum,term;printf("Input x:");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);}#include<>main(){int x=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++;}}}/* int x,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;}}}*/#include<>main(){int i,n;long p=1,m=1;printf("Input n:");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);}}#include<>main(){float c,f;for(c=-40;c<=110;c=c+10){f=9/5*c+32;printf("f=%f\n",f);}}#include<>#include<>main(){int n;double c=,x;do{x=x*pow(1+c,12)-1000;n++;}while(x>0);printf("x=%d\n",x);}#include<>main(){int n=0;float a=,c;printf("Inputc:");scanf("%f",&c);do{a=a*(1+c);n++;}while(a<=200);printf("n=%d\n",n);}#include<>#include<>main(){int n=1,count=1;double sum=1,term=1;while(fabs(term)>=1e-5){term=pow(-1,count)*(n+2));sum=sum+term;n=n+2;count++;}sum=4*sum;printf( "sum=%f,count=%d\n",sum,count); }#include<>#include<>main(){int n=1,count=1;double sum=1,term=1;while(fabs(term)>=1e-5){term=term*n);sum=sum+term;n++;count++;}printf("sum=%f,count=%d\n",sum,count); }#include<>#include<>main(){int x;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<>main(){int i=0,n;long sum=0,term=1;printf("Inputn:");scanf("%d",&n);do{i++;term=term*i;sum=sum+term;}while(sum<n);printf("%d\n",i-1);}#include<>main(){int i,n,m,count=0,sum=0;printf("Input n:");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);}}#include<>main(){int i,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);}}#include<>main(){int x,y,z;for(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);}}}#include<>main(){int x,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);}}}#include<>main(){int x,y,z;for(x=0;x<=20;x++){for(y=0;y<=33;y++){3*y+5*x+z/==100;z=100-x-y;if (5*x+3*y+z/==100&&z+x+y==100){printf("x=%d,y=%d,z=%d\n",x,y,z);}}}}#include<>main(){int x,y,z;for(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);}}}}#include<>int Square(int i){return i*i;}int main(){int i=0;i=Square(i);for( ;i<3;i++){static int i=1;i+=Square(i);printf("%d,",i);}printf("%d\n",i);return 0;}#include<>int hour,minute,second;void update(){second++;if(second==60){second=0;minute++;}if(minute==60){minute=0;hour++;}if(hour==24)hour=0;}void display(){printf("%d,%d,%d\n",hour,minute,second);}void delay(){int t;for(t=0;t<0;t++);}int main(){int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){update();display();delay();}return 0;}#include<>int GetMax(int a,int b);int main(){int x,y,max;printf("Inputx,y:");scanf("%d,%d",&x,&y);max=GetMax(x,y);printf("max=%d\n",max);return 0;}int GetMax(int m,int n){if(m>=n)return m;elsereturn n;}#include<>int LCM(int n,int m);int main(){int a,b;printf("Inputa,b:");scanf("%d,%d",&a,&b);printf("%d\n",LCM(a,b));return 0;}int LCM(int n,int m){int x;int find=0;for(x=1;!find;x++){if(x%n==0&&x%m==0){find=1;}}return x-1;}#include<>long Fact(int n);int main(){int m,a;printf("Inputm:");scanf("%d",&m);for(a=1;a<=m;a++){printf("%d!=%ld\n",a,Fact(a));}return 0;}long Fact(int n){int i;long result=1;for(i=2;i<=n;i++)result*=i;return result;}#include<>long Fact(int n);int main(){int m;long ret;printf("Inputm:");scanf("%d",&m);ret=Fact(m);printf("ret=%d\n",ret);return 0;}long Fact(int n){int i;long result=1,sum=0;for(i=2;i<=n;i++){result*=i;sum=sum+result;}return sum;}(1) #include<>int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return 0;}int Gcd(int a,int b){int t,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 t;}find=1;}(2) #include<>int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return 0;}int Gcd(int a,int b){int r,temp;r=a%b;if(r==0)return b;elsedo{temp=b;b=r;a=temp;r=a%b;}while(r!=0);return b;}(1) #include<>void Func(int x){x=2;}int main(){int x=10;Func(x);printf("%d",x);return 0;}(2) #include<>void Func(int b[]){int j;for(j=0;j<4;j++){b[j]=j;}}int main(){static int a[]={5,6,7,8},i;Func(a);for(i=0;i<4;i++){printf("%d",a[i]);}return 0;}(1) int PositiveNum(int a[],int n) {int i,count=0;for(i=0;i<n;i++){if(a[i]>0)count++;}return 0;}(2) void Fib(long f[],long n){int i;f[0]=0;f[1]=1;for(i=2;i<n;i++){f[i]=f[i-1]+f[i-2];}}(3) #include<>int main(){int a[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;}else if(a[n]<min){min=a[n];minPos=n;}}printf("max=%d,pos=%d\n",max,maxPos);printf("min=%d,pos=%d\n",min,minPos);return 0;}void DivArray(int *pArray,int n){int i;for(i=0;i<n;i++){pArray[i]/=pAttay[0];}}#include<>#define N 40int FailNum(int score[],int n);void ReadScore(int score[],int n);int main(){int score[N],n;printf("Input n:");scanf("%d",&n);ReadScore(score,n);FailNum(score,n);printf("FailNum students are %d\n",FailNum(score,n));return 0;}int FailNum(int score[],int n){int i,count=0;for(i=0;i<n;i++){if(score[i]<60)count++;}return count;}void ReadScore(int score[],int n){int i;printf("Input score:");for(i=0;i<n;i++){scanf("%d",&score[i]);}}#include<>#define N 40int HighAver(int score[],int n);int ReadScore(int score[]);int main(){int score[N],n;n=ReadScore(score);printf("HighAver students are %d\n",HighAver(score,n));return 0;}int HighAver(int score[],int n){int i,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++;}return count;}int ReadScore(int score[]){int i=-1;do{i++;printf("Input score:");scanf("%d",&score[i]);}while(score[i]>=0);return i;}#include<>#define N 40int ReadScore(int score[],long num[]);int FindMax(int score[],long num[],int n);int main(){int score[N],n,max,num,t;long num[N];t=FindMax(score,n);max=score[t];num=num[t];printf("max=%d,num=%d\n",max,num);return 0;}int ReadScore(int score[],long num[]){int i=-1;do{i++;printf("Input student's ID and score:");scanf("%ld%d",&num[i],&score[i]);}while(num[i]>0&&score[i]>=0);return i;}int FindMax(int score[],long num[],int n){int max,i;max=score[0];for(i=1;i<n;i++){if(score[i]>max)max=score[i];}return i;}#include<>#define N 40int Read(int a[]);int ChangeArry(int a[],int n);int main(){int a[N],n;printf("%d\n",ChangeArry(a,n));return 0;}int Read(int a[]){int i;for(i=0;i<9;i++){printf("Input a:");scanf("%d",&a[i]);return i;}int ChangeArry(int a[],int n){int max,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;}return a[i];}#include<>#define N 5void main(){int a[N][N];int sum = 0;int i, j;printf("请输入一个%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(i == j || i + j == N - 1)sum=sum+a[i][j];}}printf("对角线元素之和为:%d\n",sum);}#include<>#define N 2#define M 3void main(){int a[N][M],b[N][M];int i,j,sum1=0,sum2=0,sum=0;printf("请输入一个%d*%d的矩阵\n",N,M);for(i = 0; i<N; i++){for(j = 0; j< M; j++){scanf("%d",&a[i][j]);sum1=sum1+a[i][j];}}printf("请输入一个%d*%d的矩阵\n",N,M);for(i = 0; i <N; i++){for(j = 0; j <M; j++){scanf("%d",&b[i][j]);sum2=sum2+a[i][j];}}sum=sum1+sum2;printf("sum=%d\n",sum);}#include<>#define N 7void YangHui(int a[N][N],int n);void main(){int i,j,n,b[N][N];printf("Input n:");scanf("%d",&n);YangHui(b,n);for(i=0;i<n;i++){for(j=0;j<=i;j++){printf("%4d",b[i][j]);}printf("\n");}}void YangHui(int a[N][N],int n){int i,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];}}#include<>#define N 12void main(){int i;int f[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]);}}#include<>#include<>#include<>#define N 6000void main(){int a[N],i;int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0;float p1,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/;p2=(float)(b2/;p3=(float)(b3/;p4=(float)(b4/;p5=(float)(b5/;p6=(float)(b6/;}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);}(1) void Swap(int *x,int *y){int *pTemp;.\n");else if(strcmp(userInput,password)<0)printf("Invalid password!user input<password...\n");elseprintf("Invalid password!user input>password...\n");return 0;}#include<>#define N 24unsigned int CountLetter(char str[]);{char a[N];printf("Input a letter:\n");gets(a);printf("The length of the letter is:%d\n",CountLetter(a));return 0;}unsigned int CountLetter(char str[]){char *p=str;int c=0,flag=0;while(*p!='\0'){if(*p!=' ')flag=1;else if(flag==1){c++;flag=0;}p++;}return c+1;}#include<>typedef struct data{int year;int month;int day;}DATA;typedef struct work{char a[14];char b[10];char c[6];}WORK;typedef struct student{char studentName[10];char studentSex;DATA birthday;WORK profession;int main(){STUDENT stu={"王刚",'M',{1991,5,19},{"信息工程系","学习人员","学生"}};printf("stu:%2s%3c%6d/%02d/%02d%4s,%4s,%4s\n",,, return 0; }#include<>#include<>#define N 10typedef struct people{char peopleName[10];}PEOPLE;int main(){int sum1=0,sum2=0,sum3=0,sum4=0;int i;char name[N][N];PEOPLE stu[5]={"zhang","li","wang"};printf("Enter their name:\n");for(i=0;i<10;i++){gets(name[i]);if(strcmp(name[i],stu[0].peopleName)==0)sum1++;else if(strcmp(name[i],stu[1].peopleName)==0)sum2++;else if(strcmp(name[i],stu[2].peopleName)==0)sum3++;elsesum4++;}printf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);printf("stu:%4s%2d\n",stu[1].peopleName,sum2);printf("stu:%4s%2d\n",stu[2].peopleName,sum3);printf("%2d\n",sum4);return 0;}。

苏小红c语言程序设计第二版课后答案

苏小红c语言程序设计第二版课后答案

苏小红c语言程序设计第二版课后答案苏小红教授所著的《C语言程序设计》第二版是计算机科学与技术专业学生学习C语言的经典教材之一。

该书不仅系统地介绍了C语言的基础知识,还通过丰富的实例和练习题帮助学生加深理解。

以下是部分课后习题的答案,以供参考:第一章:C语言概述1. 问题一:简述C语言的特点。

- 答案: C语言是一种结构化编程语言,具有高效性、灵活性和可移植性等特点。

它支持多种数据类型,拥有丰富的运算符,并且能够直接访问内存地址。

2. 问题二:为什么说C语言是高级语言?- 答案: C语言被称为高级语言是因为它抽象了底层硬件操作,使得程序员可以不必关心计算机的硬件细节,而专注于程序的逻辑结构。

第二章:数据类型、运算符和表达式1. 问题一:说明C语言中基本数据类型的种类及其特点。

- 答案: C语言中的基本数据类型包括整型(int)、字符型(char)、浮点型(float和double)。

每种类型都有其特定的取值范围和占用内存大小。

2. 问题二:解释赋值运算符的右结合性。

- 答案:赋值运算符的右结合性意味着当多个赋值运算符同时出现时,从右向左进行计算。

例如,a = b = c; 会先计算 b = c,然后将结果赋值给 a。

第三章:控制结构1. 问题一:什么是条件语句?举例说明。

- 答案:条件语句是程序中根据条件是否满足来决定执行哪一段代码的结构。

例如,if语句就是典型的条件语句。

2. 问题二:循环语句有哪些?它们各自的特点是什么?- 答案: C语言中有三种循环语句:for循环、while循环和do-while循环。

for循环常用于已知循环次数的情况;while循环适用于循环次数未知,需要在循环体内部判断是否继续循环;do-while循环至少执行一次循环体,然后在循环末尾判断是否继续。

第四章:数组1. 问题一:一维数组和多维数组的区别是什么?- 答案:一维数组是线性的,只有一个索引来访问数组元素;而多维数组可以看作是数组的数组,需要多个索引来定位元素。

《C语言程序设计》(苏小红)课后习题答案高等教育出版社.docx

《C语言程序设计》(苏小红)课后习题答案高等教育出版社.docx

3.1(1)#i nclude<stdio.h> mai n() {int a=12,b=3;float x=18.5,y=4.6;Prin tf("%f\n",(float)(a*b)/2); Prin tf("%d ∖n",(i nt)x%(i nt)y); } 3.1(2)#i nclude<stdio.h> mai n() {int x=32,y=81,p,q; p=x++; q=--y;Prin tf("%d %d ∖n",p,q); Prin tf("%d %d ∖n",x,y);} 3.2#i nclude<stdio.h> mai n() {int x,bθ,b1,b2,s; Prin tf("I nputx:"); SCa nf("%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;2.2 #i nclude<stdio.h> mai n() { float x=2.5,y=2.5,z=2.5; Prin tf("x=%f ∖n",x); Prin tf("y=%f\n",y);Prin tf("z=%f\n",z);}L r G :V 乍 1\启言訪ett□M7V∙加炬=Ξ.539Qβ3⅞.50θβ09Pn)CC3s C)Cited after H .365 XeCnndw Uith retIlrn UdlUlC 6fe ⅛任琳续…Prin tf("s=%d ∖n",s); } 3.3#i nclude<stdio.h> #in clude<math.h> mai n() {float rate=0.0225;float n, CaPital,deposit;Prin tf("I nput n, capital:"); SCa nf("%f,%f', &n,&capital);deposit=capital*pow(1+rate, n); Prin tf("deposit=%f\n",deposit); } 3.4#i nclude<stdio.h> #in clude<math.h> mai n() {float a,b,c; double x,y;Prin tf("I nputa,b,c:");SCa nf("%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); Prin tf("x=%f,y=%f\n",x,y); }4.1 (2)#i nclude<stdio.h> mai n() {int a=12,b=15; Prin习题44.1(1)#i nclude<stdio.h> mai n() {Char C 仁'a',c2='b',c3='c'; Printf("a%cb%cc%c ∖n",c1,c2,c3);1 G:\2O17-2O18gl 学期工作 l∖CitW4∙=3)⅛nc -udeA S fdio∙hvmain()宀infab SCanf(=%2d%*2s %2d =ooa⅛ob=Pllmf(=%d⅛2n=pbx4∙2 ⅛nc -ude A S fdio∙hvmain() 宀ongab f o a f ><y 八SCanf(=%d -%2rτ⅛oa ⅛ob x SCanf (=%→>%f∖rr⅛o x⅛oy )八PIlnff(=aH%d-bH%2rΓ0b)八PII nff (=X H %→>b H %f ∖n =x⅛八5」⅛nc -ude A Sfdio∙hvf o a f BPIlnff (=-n p uω√SCanf(=%f⅛a)八if(avH0)宀a dPilnff(=aH%f∖n=a)八e-se宀ωH i -Pilnff(=aH%f∖n=a)八5.2⅛nc -ude A Sfdio∙hvmain() 宀i n ωPIlnff (=-n p u ω√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(){intyear,flag;printf("Inputayear:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0){flag=1;}else{flag=0;}if(flag==1){ printf("%disaleapyear!\n",year);}else{ printf("%disnotaleapyear!\n",year);}}5.6#include<stdio.h>main(){intyear,flag;printf("Inputayear:");scanf("%d",&year);flag=year%400==0||year%4==0&&year%100!=0?1:0;if(flag==1&&flag!=0){printf("%disaleapyear!\n",year);}else{ printf("%disnotaleapyear!\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(){intyear,month;printf("Inputyear,month:"); scanf("%d,%d",&year,&month);if(month>12||month<=0){printf("errormonth\n");}else{switch(year,month){case12:case10:case8:case7:case5:case3:case1:Prin tf("31 天∖n");break;case11:case9: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(){longi;longterm,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);}}6.11#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(){intx,y,z;for(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(){intx,y,z;for(x=0;x<=20;x++){for(y=0;y<=33;y++){3*y+5*x+z/3.0==100;z=100-x-y;if(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(){intx,y,z;for(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(inti){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(){intt;for(t=0;t<100000000;t++);}intmain(){inti;voidupdaye(),display(),delay();for(i=0;i<1000000;i++){update();display();delay();}return0;}7.3#include<stdio.h> intGetMax(inta,intb);intmain(){intx,y,max;printf("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){inti; longresult=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){inti; longresult=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) returnt;} 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,temp; r=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[]){intj;for(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(int*pArray,intn){inti;{for(i=0;i<n;i++){ pArray[i]/=pAttay[0];}}8.4#include<stdio.h>#defineN40 intFailNum(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)inti;printf("Inputscore:"); for(i=0;i<n;i++){scanf("%d",&score[i]);}}8.5#include<stdio.h>#defineN40 intHighAver(intscore[],intn); intReadScore(intscore[]); intmain(){ intscore[N],n; n=ReadScore(score);printf("HighAverstudentsare%d\n",HighAver(score,n));return0;} intHighAver(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);returni;}8.6#include<stdio.h>#defineN40 intReadScore(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);returni;}intFindMax(intscore[],longnum[],intn){intmax,i;max=score[0];for(i=1;i<n;i++){if(score[i]>max)max=score[i];}returni;}8.7#include<stdio.h>#defineN40intRead(inta[]);intChangeArry(inta[],intn);intmain(){inta[N],n;printf("%d\n",ChangeArry(a,n));return0;}intRead(inta[]){inti;for(i=0;i<9;i++){printf("Inputa:");scanf("%d",&a[i]);returni;}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;inti,j;Printf("请输入一个%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(i==j||i+j==N-1) sum=sum+a[i][j];}}printf(" 对角线元素之和为:%d\n",sum);}8.11#include<stdio.h>#defineN2#defineM3{voidmain(){inta[N][M],b[N][M];inti,j,sum1=0,sum2=0,sum=0;Printf("请输入一个%d*%d 的矩阵∖n",N,M);for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&a[i][j]);sum1=sum1+a[i][j];}}Printf("请输入一个%d*%d 的矩阵∖n",N,M);for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&b[i][j]);sum2=sum2+a[i][j];}}sum=sum1+sum2;Printf("sum=%d∖n",sum);}8.12#include<stdio.h>#defineN7voidYangHui(inta[N][N],intn);voidmain(){inti,j,n,b[N][N];Printf("InPutn:");scanf("%d",&n);YangHui(b,n);for(i=0;i<n;i++)for(j=0;j<=i;j++){printf("%4d",b[i][j]);}printf("\n");}}voidYangHui(inta[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>#defineN12 voidmain() {inti;intf[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> #defineN6000 voidmain() { 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*y){int*pTemp;//*pTemp 未进行初始化,*pTemp 指向哪里位置*pTemp=*x;*x=*y;*y=*pTemp;}9.1(2)voidSwap(int*x,int*y)int*pTemp; pTemp=x; x=y; {y=pTemp;}//错误,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};inti;printf("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*y)inttemp;temp=*x;*x=*y;*y=temp;}9.4#include<stdio.h>#defineN10 voidFindMaxMin(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){inti;*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(char*dstStr,constchar*srcStr)while((*dstStr++=*srcStr++)!='\0'){}}10.2#include<stdio.h>main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);}10.3(1)unsignedintMyStrlen(char*p)main(){unsignedintlen;len=0;for(;*pa!='\0';pa++){len++;}returnlen;}10.3(2)unsignedintMyStrlen(chars[]){char*p=s;while(*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[]){inti;for(i=0;s[i]==t[i];i++){if(s[i]=='\0')return0;}returni;}10.3(5)#include<stdio.h>#include<string.h> intmain(){ charpassword[7]="secret"; charuserInput[81]; printf("InputPassword:"); scanf("%s",userInput); if(strcmp(userInput,password)==0) printf("Coorrectpassword!Welcometothesystem...\n");elseif(strcmp(userInput,password)<0) printf("Invalidpassword!userinput<password...\n");elseprintf("Invalidpassword!userinput>password...\n"); return0;} 10.4#include<stdio.h> #defineN24 unsignedintCountLetter(charstr[]);intmain(){chara[N]; printf("Inputaletter:\n");gets(a); printf("Thelengthoftheletteris:%d\n",CountLetter(a)); return0;} unsignedintCountLetter(charstr[]){ char*p=str; intc=0,flag=0; while(*p!='\0') { if(*p!='') flag=1;elseif(flag==1){c++; flag=0;}p++;}returnc+1;}12.1#include<stdio.h>typedefstructdata{intyear;intmonth;intday;}DATA;typedefstructwork{chara[14];charb[10];charc[6];}WORK; typedefstructstudent{charstudentName[10];charstudentSex;DATAbirthday;WORKprofession;}STUDENT;intmain(){STUDENTStu={"王刚",'M',{1991,5,19},{"信息工程系","学习人员","学生"}};printf("stu:%2s%3c%6d/%02d/%02d%4s,%4s,%4s\n",stu.studentName,stu.studentSex,st u.birthday.year,Stu.birthday.month,Stu.birthday.day,Stu.profeSSion.a,Stu.profeSSion.b,Stu.profeSSion.c);return0;}12.4#include<Stdio.h>#include<String.h>#defineN10typedefStructpeople{charpeopleName[10];}PEOPLE;intmain(){intSum1=0,Sum2=0,Sum3=0,Sum4=0;inti;charname[N][N];PEOPLEStu[5]={"zhang","li","wang"};printf("Entertheirname:\n");for(i=0;i<10;i++){getS(name[i]);if(strcmp(name[i],stu[0].peopleName)==0)sum1++;elseif(strcmp(name[i],stu[1].peopleName)==0) sum2++;elseif(strcmp(name[i],stu[2].peopleName)==0)sum3++;elsesum4++;}printf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);printf("stu:%4s%2d\n",stu[1].peopleName,sum2);printf("stu:%4s%2d\n",stu[2].peopleName,sum3);printf("%2d\n",sum4);return0;}1。

《C语言程序设计》(苏小红)-课后习题答案-高等教育出版社

《C语言程序设计》(苏小红)-课后习题答案-高等教育出版社

2.2#include<stdio.h>main(){float x=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(){int a=12,b=3;float x=18.5,y=4.6;}3.1(2)#include<stdio.h>main(){int x=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(){int x,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(){float rate=0.0225;float n,capital,deposit;printf("Input n,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(){float a, b, c;double x, y;printf("Input a, 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(){char c1='a',c2='b',c3='c';}4.1(2)#include<stdio.h>main(){int a=12,b=15;}4.1(3)#include<stdio.h>main(){int a,b;printf("%d,%d\n",a,b);}4.2#include<stdio.h>main(){long a,b;float x,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(){float a;printf("Input a:");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(){int a;printf("Input a:");scanf("%d", &a);if(a%2==0){printf("a是偶数");}else{printf("a是奇数");}}5.3#include<stdio.h>#include<math.h>main(){float a,b,c,s,area;printf("Input a, 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(){float a,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);}else if(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(){int year,flag;printf("Input a year:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0){flag=1;}else{flag=0;}if(flag==1){printf("%d is a leap year!\n",year);}else{printf("%d is not a leap year!\n",year);}}5.6#include<stdio.h>main(){int year,flag;printf("Input a year:");scanf("%d",&year);flag=year%400==0||year%4==0&&year%100!=0?1:0;if(flag==1&&flag!=0){printf("%d is a leap year!\n",year);}else{printf("%d is not a leap year!\n",year);}}5.7#include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}else if(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(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch是数字字符\n");}else if(ch>=65&&ch<=90){printf("ch是大写字母\n");}else if(ch>=97&&ch<=122){printf("ch是小写字母\n");}else if(ch==32){printf("ch是空格\n");}else{printf("ch是其他字符\n");}}5.9#include<stdio.h>main(){int score,grade;printf("Input score:");scanf("%d",&score);grade=score/10;if(score<0||score>100){printf("Input error\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90){printf("%d--B\n",score);}else if(score>=70&&score<80){printf("%d--C\n",score);}else if(score>=60&&score<70){printf("%d--D\n",score);}else if(score>=0&&score<60){printf("%d--E\n",score);}}5.10#include<stdio.h>main(){int year,month;printf("Input year,month:");scanf("%d,%d",&year,&month);if(month>12||month<=0){printf("error month\n");}else{switch(year,month){case12:case10:case8:case7:case5:case3:case1:printf("31天\n");break;case11:case9: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("Input error\n");}}}6.1(1)#include<stdio.h>main(){int i,j,k;char space='';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(){int k=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(){int k=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(){int i,sum=0;for(i=1;i<=101;i++){sum=sum+i;}printf("sum=%d\n",sum);}6.2(2)#include<stdio.h>main(){long i;long term,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(){int n=1;float term=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(){int n=1,count=1;float x;double sum,term;printf("Input x:");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(){int x=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++;}}}/*int x,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(){int i,n;long p=1,m=1;printf("Input n:");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(){float c,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(){int n;double c=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(){int n=0;float a=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(){int n=1,count=1;double sum=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(){int n=1,count=1;double sum=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(){int x;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);}}6.11#include<stdio.h>main(){int i=0,n;long sum=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(){int i,n,m,count=0,sum=0;printf("Input n:");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(){int i,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(){int x,y,z;for(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(){int x,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(){int x,y,z;for(x=0;x<=20;x++){for(y=0;y<=33;y++){3*y+5*x+z/3.0==100;z=100-x-y;if(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(){int x,y,z;for(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>int Square(int i){return i*i;}int main(){int i=0;i=Square(i);for(;i<3;i++){static int i=1;i+=Square(i);printf("%d,",i);}printf("%d\n",i);return0;}7.2#include<stdio.h>int hour,minute,second;void update(){second++;if(second==60){second=0;minute++;}if(minute==60){minute=0;hour++;}if(hour==24)hour=0;}void display(){printf("%d,%d,%d\n",hour,minute,second); }void delay(){int t;for(t=0;t<100000000;t++);}int main(){int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){update();display();delay();}return0;}7.3#include<stdio.h>int GetMax(int a,int b);int main(){int x,y,max;printf("Inputx,y:");scanf("%d,%d",&x,&y);max=GetMax(x,y);printf("max=%d\n",max);return0;}int GetMax(int m,int n){if(m>=n)return m;elsereturn n;}7.4#include<stdio.h>int LCM(int n,int m);int main(){int a,b;printf("Inputa,b:");scanf("%d,%d",&a,&b);printf("%d\n",LCM(a,b));return0;}int LCM(int n,int m){int x;int find=0;for(x=1;!find;x++){if(x%n==0&&x%m==0){find=1;}}return x-1;}7.5#include<stdio.h>long Fact(int n);int main(){int m,a;printf("Inputm:");scanf("%d",&m);for(a=1;a<=m;a++){printf("%d!=%ld\n",a,Fact(a));}return0;}long Fact(int n){int i;long result=1;for(i=2;i<=n;i++)result*=i;return result;}7.6#include<stdio.h>long Fact(int n);int main(){int m;long ret;printf("Inputm:");scanf("%d",&m);ret=Fact(m);printf("ret=%d\n",ret);return0;}long Fact(int n){int i;long result=1,sum=0;for(i=2;i<=n;i++){result*=i;sum=sum+result;}return sum;}7.7(1)#include<stdio.h>int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}int Gcd(int a,int b){int t,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 t;}find=1;}7.7(2)#include<stdio.h> int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}int Gcd(int a,int b){int r,temp;r=a%b;if(r==0)return b;elsedo{temp=b;b=r;a=temp;r=a%b;}while(r!=0);return b;}8.1(1)#include<stdio.h> void Func(int x){x=2;}int main(){int x=10;Func(x);printf("%d",x);return0;}8.1(2)#include<stdio.h> void Func(int b[]){int j;for(j=0;j<4;j++){b[j]=j;}}int main(){static int a[]={5,6,7,8},i;Func(a);for(i=0;i<4;i++){printf("%d",a[i]);}return0;}8.2(1)int PositiveNum(int a[],int n) {int i,count=0;for(i=0;i<n;i++){if(a[i]>0)count++;}return0;}8.2(2)void Fib(long f[],long n){int i;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>int main(){int a[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;}else if(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.3void DivArray(int*pArray,int n){int i;for(i=0;i<n;i++){pArray[i]/=pAttay[0];}}8.4#include<stdio.h>#define N40int FailNum(int score[],int n);void ReadScore(int score[],int n);int main(){int score[N],n;printf("Input n:");scanf("%d",&n);ReadScore(score,n);FailNum(score,n);printf("FailNum students are%d\n",FailNum(score,n));return0;}int FailNum(int score[],int n){int i,count=0;for(i=0;i<n;i++){if(score[i]<60)count++;}return count;}void ReadScore(int score[],int n){int i;printf("Input score:");for(i=0;i<n;i++){scanf("%d",&score[i]);}}8.5#include<stdio.h>#define N40int HighAver(int score[],int n);int ReadScore(int score[]);int main(){int score[N],n;n=ReadScore(score);printf("HighAver students are%d\n",HighAver(score,n));return0;}int HighAver(int score[],int n){int i,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++;}return count;}int ReadScore(int score[]){int i=-1;do{i++;printf("Input score:");scanf("%d",&score[i]);}while(score[i]>=0);return i;}8.6#include<stdio.h>#define N40int ReadScore(int score[],long num[]);int FindMax(int score[],long num[],int n);int main(){int score[N],n,max,num,t;long num[N];t=FindMax(score,n);max=score[t];num=num[t];printf("max=%d,num=%d\n",max,num);return0;}int ReadScore(int score[],long num[]){int i=-1;do{i++;printf("Input student's ID and score:");scanf("%ld%d",&num[i],&score[i]);}while(num[i]>0&&score[i]>=0);return i;}int FindMax(int score[],long num[],int n){int max,i;max=score[0];for(i=1;i<n;i++){if(score[i]>max)max=score[i];}return i;}8.7#include<stdio.h>#define N40int Read(int a[]);int ChangeArry(int a[],int n);int main(){int a[N],n;printf("%d\n",ChangeArry(a,n));return0;}int Read(int a[]){int i;for(i=0;i<9;i++){printf("Input a:");scanf("%d",&a[i]);return i;}int ChangeArry(int a[],int n){int max,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;}return a[i];}8.10#include<stdio.h>#define N5void main(){int a[N][N];int sum=0;int i,j;printf("请输入一个%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(i==j||i+j==N-1)sum=sum+a[i][j];}}printf("对角线元素之和为:%d\n",sum);}8.11#include<stdio.h>#define N2#define M3void main(){int a[N][M],b[N][M];int i,j,sum1=0,sum2=0,sum=0;printf("请输入一个%d*%d的矩阵\n",N,M);for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&a[i][j]);sum1=sum1+a[i][j];}}printf("请输入一个%d*%d的矩阵\n",N,M);for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&b[i][j]);sum2=sum2+a[i][j];}}sum=sum1+sum2;printf("sum=%d\n",sum);}8.12#include<stdio.h>#define N7void YangHui(int a[N][N],int n);void main(){int i,j,n,b[N][N];printf("Input n:");scanf("%d",&n);YangHui(b,n);for(i=0;i<n;i++){for(j=0;j<=i;j++){printf("%4d",b[i][j]);}printf("\n");}}void YangHui(int a[N][N],int n){int i,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>#define N12void main(){int i;int f[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>#define N6000void main(){int a[N],i;int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0;float p1,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)void Swap(int*x,int*y){int*pTemp;//*pTemp未进行初始化,*pTemp指向哪里位置*pTemp=*x;*x=*y;*y=*pTemp;}9.1(2)void Swap(int*x,int*y){int*pTemp;pTemp=x;x=y;y=pTemp;}//错误,x,y没有指向类型9.2#include<stdio.h>int main(){int a[]={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>void Swap(int*x,int*y);int main(){int a[6]={1,2,3,4,5,6},b[6]={11,22,33,44,55,66};int i;printf("Before Array\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("After Array\n");for(i=0;i<6;i++){printf("%d\n",a[i]);}for(i=0;i<6;i++){printf("%d\n",b[i]);}return0;}void Swap(int*x,int*y){int temp;temp=*x;*x=*y;*y=temp;}9.4#include<stdio.h>#define N10void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum);int main(){int a[N];int i,maxa,mina,maxnum,minnum;printf("Input a:");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;}void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum){int i;*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;}else if(a[i]<*pMina){*pMina=a[i];*pMinnum=i;}}}10.1void MYStrcpy(char*dstStr,const char*srcStr){while((*dstStr++=*srcStr++)!='\0'){}}10.2#include<stdio.h>main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);} 10.3(1)unsigned int MyStrlen(char*p)main(){unsigned int len;len=0;for(;*pa!='\0';pa++){len++;}return len;}10.3(2)unsigned int MyStrlen(char s[]){char*p=s;while(*p!='\0'){p++;}return(p-*p);}10.3(3)int MyStrcmp(char*p1,char*p2){for(;*p1==*p2;p1++,p2++){if(*p1=='\0')return-1;}return1;}10.3(4)int MyStrcmp(char s[],char t[]){int i;for(i=0;s[i]==t[i];i++){if(s[i]=='\0')return0;}return i;}10.3(5)#include<stdio.h>#include<string.h>int main(){char password[7]="secret";char userInput[81];printf("Input Password:");scanf("%s",userInput);if(strcmp(userInput,password)==0)printf("Coorrect password!Welcome to the system...\n");else if(strcmp(userInput,password)<0)printf("Invalid password!user input<password...\n");elseprintf("Invalid password!user input>password...\n");return0;}10.4#include<stdio.h>#define N24unsigned int CountLetter(char str[]);int main(){char a[N];printf("Input a letter:\n");gets(a);printf("The length of the letter is:%d\n",CountLetter(a));return0;}unsigned int CountLetter(char str[]){char*p=str;int c=0,flag=0;while(*p!='\0'){if(*p!='')flag=1;else if(flag==1){c++;flag=0;}p++;}return c+1;}12.1#include<stdio.h>typedef struct data{int year;int month;int day;}DATA;typedef struct work{char a[14];char b[10];char c[6];}WORK;typedef struct student{char studentName[10];char studentSex;DATA birthday;WORK profession;}STUDENT;int main(){STUDENT stu={"王刚",'M',{1991,5,19},{"信息工程系","学习人员","学生"}};printf("stu:%2s%3c%6d/%02d/%02d%4s,%4s,%4s\n",stu.studentName,stu.studentSex,st u.birthday.year,stu.birthday.month,stu.birthday.day,stu.profession.a,stu.profession.b,stu.profession.c);return0;}12.4#include<stdio.h>#include<string.h>#define N10typedef struct people{char peopleName[10];}PEOPLE;int main(){int sum1=0,sum2=0,sum3=0,sum4=0;int i;char name[N][N];PEOPLE stu[5]={"zhang","li","wang"};printf("Enter their name:\n");for(i=0;i<10;i++){gets(name[i]);if(strcmp(name[i],stu[0].peopleName)==0)sum1++;else if(strcmp(name[i],stu[1].peopleName)==0) sum2++;else if(strcmp(name[i],stu[2].peopleName)==0) sum3++;elsesum4++;}printf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);printf("stu:%4s%2d\n",stu[1].peopleName,sum2);printf("stu:%4s%2d\n",stu[2].peopleName,sum3);printf("%2d\n",sum4);return0;}。

最新《C语言程序设计》(苏小红) 课后习题答案 高等教育出版社资料

最新《C语言程序设计》(苏小红) 课后习题答案 高等教育出版社资料

2.2 #include<stdio.h>main(){float x=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(){int a=12,b=3;float x=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(){int x=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(){int x,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(){float rate=0.0225;float n,capital,deposit;printf("Input n,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(){float a, b, c;double x, y;printf("Input a, 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);}习题4 Array 4.1(1)#include<stdio.h>main(){char c1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3);}4.1(2)main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b);}#include<stdio.h>main(){int a,b;printf("%d,%d\n",a,b);}4.2#include<stdio.h>main(){long a,b;float x,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(){float a;printf("Input a:");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(){int a;printf("Input a:");scanf("%d", &a);if(a%2==0)printf("a是偶数");}else{printf("a是奇数");}}5.3#include<stdio.h>#include<math.h>main(){float a,b,c,s,area;printf("Input a, 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(){float a,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);}else if(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(){int year,flag;printf("Input a year:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0){flag=1;}else{flag=0;}if(flag==1){printf("%d is a leap year!\n",year);}else{printf("%d is not a leap year!\n",year);}}5.6#include<stdio.h>main(){int year,flag;printf("Input a year:");scanf("%d",&year);flag=year%400==0||year%4==0&&year%100!=0?1:0;if(flag==1&&flag!=0){printf("%d is a leap year!\n",year);}else{printf("%d is not a leap year!\n",year);}}5.7#include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}else if(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(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch是数字字符\n");}else if(ch>=65&&ch<=90){printf("ch是大写字母\n");}else if(ch>=97&&ch<=122){printf("ch是小写字母\n");}else if(ch==32){printf("ch是空格\n");}else{printf("ch是其他字符\n");}}5.9#include<stdio.h>main(){int score,grade;printf("Input score:");scanf("%d",&score);grade=score/10;if(score<0||score>100){printf("Input error\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90){printf("%d--B\n",score);}else if(score>=70&&score<80){printf("%d--C\n",score);}else if(score>=60&&score<70){printf("%d--D\n",score);}else if(score>=0&&score<60){printf("%d--E\n",score);}}5.10#include<stdio.h>main(){int year,month;printf("Input year,month:");scanf("%d,%d",&year,&month);if(month>12||month<=0){printf("error month\n");}else{switch(year,month){case12:case10:case8:case7:case5:case3:case1:printf("31天\n");break;case11:case9: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("Input error\n");}}}6.1(1)#include<stdio.h>main(){int i,j,k;char space='';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(){int k=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(){int k=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(){int i,sum=0;for(i=1;i<=101;i++){sum=sum+i;}printf("sum=%d\n",sum);}6.2(2)#include<stdio.h>main(){long i;long term,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(){int n=1;float term=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(){int n=1,count=1;float x;double sum,term;printf("Input x:");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(){int x=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++;}}}/*int x,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(){int i,n;long p=1,m=1;printf("Input n:");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(){float c,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(){int n;double c=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(){int n=0;float a=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(){int n=1,count=1;double sum=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(){int n=1,count=1;double sum=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(){int x;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);}}6.11#include<stdio.h>main(){int i=0,n;long sum=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(){int i,n,m,count=0,sum=0;printf("Input n:");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(){int i,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(){int x,y,z;for(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(){int x,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(){int x,y,z;for(x=0;x<=20;x++){for(y=0;y<=33;y++){3*y+5*x+z/3.0==100;z=100-x-y;if(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(){int x,y,z;for(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>int Square(int i){return i*i;}int main(){int i=0;i=Square(i);for(;i<3;i++){static int i=1;i+=Square(i);printf("%d,",i);}printf("%d\n",i);return0;}7.2#include<stdio.h>int hour,minute,second;void update(){second++;if(second==60){second=0;minute++;}if(minute==60){minute=0;hour++;}if(hour==24)hour=0;}void display(){printf("%d,%d,%d\n",hour,minute,second); }void delay(){int t;for(t=0;t<100000000;t++);}int main(){int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){update();display();delay();}return0;}7.3#include<stdio.h>int GetMax(int a,int b);int main(){int x,y,max;printf("Inputx,y:");scanf("%d,%d",&x,&y);max=GetMax(x,y);printf("max=%d\n",max);return0;}int GetMax(int m,int n){if(m>=n)return m;elsereturn n;}7.4#include<stdio.h>int LCM(int n,int m);int main(){int a,b;printf("Inputa,b:");scanf("%d,%d",&a,&b);printf("%d\n",LCM(a,b));return0;}int LCM(int n,int m){int x;int find=0;for(x=1;!find;x++){if(x%n==0&&x%m==0){find=1;}}return x-1;}7.5#include<stdio.h>long Fact(int n);int main(){int m,a;printf("Inputm:");scanf("%d",&m);for(a=1;a<=m;a++){printf("%d!=%ld\n",a,Fact(a));}return0;}long Fact(int n){int i;long result=1;for(i=2;i<=n;i++)result*=i;return result;}7.6#include<stdio.h>long Fact(int n);int main(){int m;long ret;printf("Inputm:");scanf("%d",&m);ret=Fact(m);printf("ret=%d\n",ret);return0;}long Fact(int n){int i;long result=1,sum=0;for(i=2;i<=n;i++){result*=i;sum=sum+result;}return sum;}7.7(1)#include<stdio.h>int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}int Gcd(int a,int b){int t,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 t;}find=1;}7.7(2)#include<stdio.h> int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}int Gcd(int a,int b){int r,temp;r=a%b;if(r==0)return b;elsedo{temp=b;b=r;a=temp;r=a%b;}while(r!=0);return b;}8.1(1)#include<stdio.h> void Func(int x){x=2;}int main(){int x=10;Func(x);printf("%d",x);return0;}8.1(2)#include<stdio.h> void Func(int b[]){int j;for(j=0;j<4;j++){b[j]=j;}}int main(){static int a[]={5,6,7,8},i;Func(a);for(i=0;i<4;i++){printf("%d",a[i]);}return0;}8.2(1)int PositiveNum(int a[],int n) {int i,count=0;for(i=0;i<n;i++){if(a[i]>0)count++;}return0;}8.2(2)void Fib(long f[],long n){int i;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>int main(){int a[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;}else if(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.3void DivArray(int*pArray,int n){int i;for(i=0;i<n;i++){pArray[i]/=pAttay[0];}}8.4#include<stdio.h>#define N40int FailNum(int score[],int n);void ReadScore(int score[],int n);int main(){int score[N],n;printf("Input n:");scanf("%d",&n);ReadScore(score,n);FailNum(score,n);printf("FailNum students are%d\n",FailNum(score,n));return0;}int FailNum(int score[],int n){int i,count=0;for(i=0;i<n;i++){if(score[i]<60)count++;}return count;}void ReadScore(int score[],int n){int i;printf("Input score:");for(i=0;i<n;i++){scanf("%d",&score[i]);}}8.5#include<stdio.h>#define N40int HighAver(int score[],int n);int ReadScore(int score[]);int main(){int score[N],n;n=ReadScore(score);printf("HighAver students are%d\n",HighAver(score,n));return0;}int HighAver(int score[],int n){int i,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++;}return count;}int ReadScore(int score[]){int i=-1;do{i++;printf("Input score:");scanf("%d",&score[i]);}while(score[i]>=0);return i;}8.6#include<stdio.h>#define N40int ReadScore(int score[],long num[]);int FindMax(int score[],long num[],int n);int main(){int score[N],n,max,num,t;long num[N];t=FindMax(score,n);max=score[t];num=num[t];printf("max=%d,num=%d\n",max,num);return0;}int ReadScore(int score[],long num[]){int i=-1;do{i++;printf("Input student's ID and score:");scanf("%ld%d",&num[i],&score[i]);}while(num[i]>0&&score[i]>=0);return i;}int FindMax(int score[],long num[],int n){int max,i;max=score[0];for(i=1;i<n;i++){if(score[i]>max)max=score[i];}return i;}8.7#include<stdio.h>#define N40int Read(int a[]);int ChangeArry(int a[],int n);int main(){int a[N],n;printf("%d\n",ChangeArry(a,n));return0;}int Read(int a[]){int i;for(i=0;i<9;i++){printf("Input a:");scanf("%d",&a[i]);return i;}int ChangeArry(int a[],int n){int max,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;}return a[i];}8.10#include<stdio.h>#define N5void main(){int a[N][N];int sum=0;int i,j;printf("请输入一个%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(i==j||i+j==N-1)sum=sum+a[i][j];}}printf("对角线元素之和为:%d\n",sum);}8.11#include<stdio.h>#define N2#define M3void main(){int a[N][M],b[N][M];int i,j,sum1=0,sum2=0,sum=0;printf("请输入一个%d*%d的矩阵\n",N,M);for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&a[i][j]);sum1=sum1+a[i][j];}}printf("请输入一个%d*%d的矩阵\n",N,M);for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&b[i][j]);sum2=sum2+a[i][j];}}sum=sum1+sum2;printf("sum=%d\n",sum);}8.12#include<stdio.h>#define N7void YangHui(int a[N][N],int n);void main(){int i,j,n,b[N][N];printf("Input n:");scanf("%d",&n);YangHui(b,n);for(i=0;i<n;i++){for(j=0;j<=i;j++){printf("%4d",b[i][j]);}printf("\n");}}void YangHui(int a[N][N],int n){int i,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>#define N12void main(){int i;int f[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>#define N6000void main(){int a[N],i;int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0;float p1,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)void Swap(int*x,int*y){int*pTemp;//*pTemp未进行初始化,*pTemp指向哪里位置*pTemp=*x;*x=*y;*y=*pTemp;}9.1(2)void Swap(int*x,int*y){int*pTemp;pTemp=x;x=y;y=pTemp;}//错误,x,y没有指向类型9.2#include<stdio.h>int main(){int a[]={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>void Swap(int*x,int*y);int main(){int a[6]={1,2,3,4,5,6},b[6]={11,22,33,44,55,66};int i;printf("Before Array\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("After Array\n");for(i=0;i<6;i++){printf("%d\n",a[i]);}for(i=0;i<6;i++){printf("%d\n",b[i]);}return0;}void Swap(int*x,int*y){int temp;temp=*x;*x=*y;*y=temp;}9.4#include<stdio.h>#define N10void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum);int main(){int a[N];int i,maxa,mina,maxnum,minnum;printf("Input a:");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;}void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum){int i;*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;}else if(a[i]<*pMina){*pMina=a[i];*pMinnum=i;}}}10.1void MYStrcpy(char*dstStr,const char*srcStr){while((*dstStr++=*srcStr++)!='\0'){}}10.2#include<stdio.h>main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);} 10.3(1)unsigned int MyStrlen(char*p)main(){unsigned int len;len=0;for(;*pa!='\0';pa++){len++;}return len;}10.3(2)unsigned int MyStrlen(char s[]){char*p=s;while(*p!='\0'){p++;}return(p-*p);}10.3(3)int MyStrcmp(char*p1,char*p2){for(;*p1==*p2;p1++,p2++){if(*p1=='\0')return-1;}return1;}10.3(4)int MyStrcmp(char s[],char t[]){int i;for(i=0;s[i]==t[i];i++){if(s[i]=='\0')return0;}return i;}10.3(5)#include<stdio.h>#include<string.h>int main(){char password[7]="secret";char userInput[81];printf("Input Password:");scanf("%s",userInput);if(strcmp(userInput,password)==0)printf("Coorrect password!Welcome to the system...\n");else if(strcmp(userInput,password)<0)printf("Invalid password!user input<password...\n");elseprintf("Invalid password!user input>password...\n");return0;}10.4#include<stdio.h>#define N24unsigned int CountLetter(char str[]);int main(){char a[N];printf("Input a letter:\n");gets(a);printf("The length of the letter is:%d\n",CountLetter(a));return0;}unsigned int CountLetter(char str[]){char*p=str;int c=0,flag=0;while(*p!='\0'){if(*p!='')flag=1;else if(flag==1){c++;flag=0;}p++;}return c+1;}12.1#include<stdio.h>typedef struct data{int year;int month;int day;}DATA;typedef struct work{char a[14];char b[10];char c[6];}WORK;typedef struct student{char studentName[10];char studentSex;DATA birthday;WORK profession;}STUDENT;int main(){STUDENT stu={"王刚",'M',{1991,5,19},{"信息工程系","学习人员","学生"}};printf("stu:%2s%3c%6d/%02d/%02d%4s,%4s,%4s\n",stu.studentName,stu.studentSex,st u.birthday.year,stu.birthday.month,stu.birthday.day,stu.profession.a,stu.profession.b,stu.profession.c);return0;}12.4#include<stdio.h>#include<string.h>#define N10typedef struct people{char peopleName[10];}PEOPLE;int main(){int sum1=0,sum2=0,sum3=0,sum4=0;int i;char name[N][N];PEOPLE stu[5]={"zhang","li","wang"};printf("Enter their name:\n");for(i=0;i<10;i++){gets(name[i]);if(strcmp(name[i],stu[0].peopleName)==0)sum1++;else if(strcmp(name[i],stu[1].peopleName)==0) sum2++;else if(strcmp(name[i],stu[2].peopleName)==0) sum3++;elsesum4++;}printf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);printf("stu:%4s%2d\n",stu[1].peopleName,sum2);printf("stu:%4s%2d\n",stu[2].peopleName,sum3);printf("%2d\n",sum4);return0;}精品文档。

苏小红c语言程序设计课后答案

苏小红c语言程序设计课后答案

苏小红c语言程序设计课后答案苏小红的《C语言程序设计》是一本广泛使用的教材,它以清晰的结构和丰富的示例,帮助学生掌握C语言的基础知识和编程技巧。

课后答案对于学生来说是一个重要的学习资源,可以帮助他们检查自己的学习成果,加深对知识点的理解。

以下是一些可能的课后答案示例,用于帮助学生复习和理解C语言程序设计的相关概念。

第一章:C语言概述1. C语言的发展历史:C语言由Dennis Ritchie在20世纪70年代初期开发,最初用于UNIX操作系统的编写。

2. C语言的特点:C语言是一种结构化语言,具有高度的灵活性和强大的功能,能够进行底层系统编程。

第二章:C语言基础1. 数据类型:C语言提供了多种数据类型,包括整型(int)、浮点型(float和double)、字符型(char)等。

2. 变量声明:变量在使用前必须声明,声明时需要指定数据类型和变量名。

第三章:运算符和表达式1. 算术运算符:包括加(+)、减(-)、乘(*)、除(/)等。

2. 赋值运算符:如`=`,`+=`,`-=`等,用于给变量赋值或进行运算后赋值。

第四章:控制结构1. 条件语句:如`if`,`else if`,`else`,用于根据不同的条件执行不同的代码块。

2. 循环语句:包括`for`循环、`while`循环和`do-while`循环,用于重复执行代码块。

第五章:数组1. 一维数组:存储相同类型的元素,可以通过下标访问数组元素。

2. 多维数组:如二维数组,可以看作是数组的数组。

第六章:函数1. 函数定义:使用`return`类型和函数名来定义函数。

2. 函数调用:通过函数名和必要的参数来调用函数。

第七章:指针1. 指针变量:存储另一个变量的内存地址。

2. 指针运算:包括地址运算和指针的增减。

第八章:结构体和联合体1. 结构体:可以包含不同类型的数据成员。

2. 联合体:所有成员共享相同的内存位置。

第九章:预处理指令1. 宏定义:使用`#define`来定义常量或代码片段。

《C语言程序设计》(苏小红) 课后习题参考答案 高等教育出版社

《C语言程序设计》(苏小红) 课后习题参考答案 高等教育出版社

printf("y=%f\n",y);printf("z=%f\n",z);}3.1(1)main(){}3.1(2)main(){}3.2main(){int x,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(){float rate=0.0225;float n,capital,deposit;printf("Input n,capital:");scanf("%f,%f",&n,&capital);deposit=capital*pow(1+rate,n);printf("deposit=%f\n",deposit); }3.4main(){}习题44.1(1)main(){}4.1(2)main(){}4.1(3)main(){}4.2#include<stdio.h>main(){long a,b;float x,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.1main(){{}{}}5.2main(){intscanf("%d", &a);if(a%2==0){printf("a是偶数");}else{printf("a是奇数");}}5.3#include<stdio.h>#include<math.h>main(){float a,b,c,s,area;printf("Input a, 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);}{}}5.4main(){{}{}else if(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(){int year,flag;printf("Input a year:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0) {flag=1;}else{}{}{}}5.6main(){int{}else{printf("%d is not a leap year!\n",year);}}5.7#include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}else if(ch>='A'&&ch<='Z'){ch=getchar();ch=ch+32;printf("%c,%d\n",ch,ch);}{}}5.8#include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch是数字字符\n");grade=score/10;if(score<0||score>100){printf("Input error\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90){printf("%d--B\n",score);}else if(score>=70&&score<80){printf("%d--C\n",score);}else if(score>=60&&score<70){printf("%d--D\n",score);}case3:case1:printf("31天\n");break;case11:case9:case6:case4:printf("30天\n");break;case2:if(year%4==0&&year!=0||year%400==0){printf("29天\n");}else{printf("28天\n");}break;main(){int k=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(){int k=4,n;for(n=0;n<k;n++){if(n%2==0)break;k--;}printf("k=%d,n=%d\n",k,n);main(){int n=1;float term=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(){int n=1,count=1;float x;}}/*int x,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(){int i,n;long p=1,m=1;printf("Input n:");scanf("%d",&n);x=x*pow(1+c,12)-1000;n++;}while(x>0);printf("x=%d\n",x);}6.7#include<stdio.h>main(){int n=0;float a=100.0,c;printf("Inputc:");scanf("%f",&c);do{a=a*(1+c);n++;}while(a<=200);printf("n=%d\n",n);}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(){int x;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);}}{sum=sum+m;count++;}else{break;}printf("sum=%d,count=%d\n",sum,count);}}6.13#include<stdio.h>main(){int i,n,m,count=0,sum=0;printf("Inputn:");scanf("%d",&n);for(i=1;i<=n;i++){printf("x=%d,y=%d,z=%d\n",x,y,z);}}}6.15#include<stdio.h>main(){int x,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>{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>int Square(int i){return i*i;}int main(){int i=0;i=Square(i);hour=0;}void display(){printf("%d,%d,%d\n",hour,minute,second); }void delay(){int t;for(t=0;t<100000000;t++);}int main(){int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){update();int main(){int a,b;printf("Inputa,b:");scanf("%d,%d",&a,&b);printf("%d\n",LCM(a,b));return0;}int LCM(int n,int m){int x;int find=0;for(x=1;!find;x++){if(x%n==0&&x%m==0){find=1;}long Fact(int n);int main(){int m;long ret;printf("Inputm:");scanf("%d",&m);ret=Fact(m);printf("ret=%d\n",ret);return0;}long Fact(int n){int i;long result=1,sum=0;for(i=2;i<=n;i++){result*=i;sum=sum+result;find=1;}7.7(2)#include<stdio.h> int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}int Gcd(int a,int b){int r,temp;r=a%b;if(r==0)return b;else{b[j]=j;}}int main(){static int a[]={5,6,7,8},i;Func(a);for(i=0;i<4;i++){printf("%d",a[i]);}return0;}8.2(1)int PositiveNum(int a[],int n) {int i,count=0;for(i=0;i<n;i++){{if(a[n]>max){max=a[n];maxPos=n;}else if(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.3void DivArray(int*pArray,int n){int i;count++;}return count;}void ReadScore(int score[],int n){int i;printf("Input score:");for(i=0;i<n;i++){scanf("%d",&score[i]);}}8.5#include<stdio.h>#define N40int HighAver(int score[],int n);int ReadScore(int score[]);int main(){printf("Input score:");scanf("%d",&score[i]);}while(score[i]>=0);return i;}8.6#include<stdio.h>#define N40int ReadScore(int score[],long num[]); int FindMax(int score[],long num[],int n);int main(){int score[N],n,max,num,t;long num[N];t=FindMax(score,n);max=score[t];num=num[t];printf("max=%d,num=%d\n",max,num);return0;}int main(){int a[N],n;printf("%d\n",ChangeArry(a,n));return0;}int Read(int a[]){int i;for(i=0;i<9;i++){printf("Input a:");scanf("%d",&a[i]);return i;}int ChangeArry(int a[],int n){int max,min,i,maxpos,minpos,term;max=min=score[0];for(j=0;j<N;j++){scanf("%d",&a[i][j]);}}for(i=0;i<N;i++){for(j=0;j<N;j++){if(i==j||i+j==N-1)sum=sum+a[i][j];}}printf("对角线元素之和为:%d\n",sum); }8.11#include<stdio.h>#define N2#define M3void main()#define N7void YangHui(int a[N][N],int n);void main(){int i,j,n,b[N][N];printf("Input n:");scanf("%d",&n);YangHui(b,n);for(i=0;i<n;i++){for(j=0;j<=i;j++){printf("%4d",b[i][j]);}printf("\n");}}void YangHui(int a[N][N],int n){}8.14#include<stdio.h>#include<stdlib.h>#include<time.h>#define N6000void main(){int a[N],i;int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0;float p1,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++;*pTemp=*x;*x=*y;*y=*pTemp;}9.1(2)void Swap(int*x,int*y) {int*pTemp;pTemp=x;x=y;y=pTemp;}//错误,x,y没有指向类型9.2#include<stdio.h>int main(){int a[]={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;for(i=0;i<6;i++){printf("%d\n",b[i]);}return0;}void Swap(int*x,int*y){int temp;temp=*x;*x=*y;*y=temp;}9.4#include<stdio.h>#define N10void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum); int main(){int a[N];{*pMina=a[i];*pMinnum=i;}}}10.1void MYStrcpy(char*dstStr,const char*srcStr){while((*dstStr++=*srcStr++)!='\0'){}}10.2#include<stdio.h>main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);} 10.3(1)unsigned int MyStrlen(char*p)main(){unsigned int len;{int i;for(i=0;s[i]==t[i];i++){if(s[i]=='\0')return0;}return i;}10.3(5)#include<stdio.h>#include<string.h>int main(){char password[7]="secret";char userInput[81];printf("Input Password:");scanf("%s",userInput);if(strcmp(userInput,password)==0)printf("Coorrect password!Welcome to the system...\n");else if(strcmp(userInput,password)<0){c++;flag=0;}p++;}return c+1;}12.1#include<stdio.h>typedef struct data{int year;int month;int day;}DATA;typedef struct work{char a[14];char b[10];int sum1=0,sum2=0,sum3=0,sum4=0;int i;char name[N][N];PEOPLE stu[5]={"zhang","li","wang"};printf("Enter their name:\n");for(i=0;i<10;i++){gets(name[i]);if(strcmp(name[i],stu[0].peopleName)==0) sum1++;else if(strcmp(name[i],stu[1].peopleName)==0) sum2++;else if(strcmp(name[i],stu[2].peopleName)==0) sum3++;elsesum4++;}printf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);。

c语言程序设计苏小红版答案

c语言程序设计苏小红版答案

c语言程序设计苏小红版答案C语言程序设计是计算机科学与技术专业学生的基础课程之一,它不仅教授了编程的基本概念和技巧,还培养了学生的逻辑思维和问题解决能力。

苏小红版的《C语言程序设计》教材以其系统性和实用性广受好评。

以下是对该书部分章节的习题答案的总结,以帮助学生更好地理解和掌握C语言程序设计。

第一章:C语言概述1. C语言的特点:C语言是一种结构化语言,具有高级语言的语法和汇编语言的控制能力。

它具有可移植性、简洁性、高效性等特点。

2. C语言的发展历史:C语言由Dennis Ritchie在20世纪70年代初期开发,最初用于UNIX操作系统。

第二章:数据类型、运算符和表达式1. 基本数据类型:包括整型(int)、浮点型(float和double)、字符型(char)等。

2. 运算符:包括算术运算符、关系运算符、逻辑运算符、位运算符等。

3. 表达式:由变量、常量、运算符和函数调用组成的,可以计算出结果的代码片段。

第三章:控制结构1. 顺序结构:程序按照编写顺序依次执行。

2. 选择结构:使用if、switch等语句根据条件选择执行不同的代码块。

3. 循环结构:使用while、do-while、for等循环语句重复执行代码块。

第四章:数组1. 一维数组:连续存储的相同类型元素集合。

2. 二维数组:可以看作是一维数组的数组,用于存储矩阵等数据结构。

3. 字符数组:特殊的一维数组,用于存储字符串。

第五章:函数1. 函数的定义:使用关键字`void`或返回类型定义函数的返回值类型。

2. 函数的调用:在程序中通过函数名和参数列表调用函数。

3. 递归函数:函数调用自身,用于解决某些递归问题。

第六章:指针1. 指针变量:存储另一个变量的内存地址的变量。

2. 指针与数组:指针可以方便地操作数组元素。

3. 函数指针:指向函数入口地址的指针变量,可以调用函数。

第七章:结构体与联合体1. 结构体:可以包含不同类型的数据成员的复合数据类型。

C语言程序设计苏小红版答案

C语言程序设计苏小红版答案

#include<>main(){float x=,y=,z=;printf("x=%f\n",x);printf("y=%f\n",y);printf("z=%f\n",z);}(1) #include<>main(){int a=12,b=3;float x=,y=;printf("%d\n",(float)(a*b)/2);printf("%d\n",(int)x%(int)y);}#include<>main(){int x,b0,b1,b2,s;printf("Inputx:");scanf("%d",&x);b2=x/100;b1=(x-b2*100)/10;b0=x%10;s=b0*100+b1*10+b2;printf("s=%d\n",s);}#include<>#include<>main(){float rate=;float n,capital,deposit;printf("Input n,capital:");scanf("%f,%f",&n,&capital);deposit=capital*pow(1+rate,n);printf("deposit=%f\n",deposit); }#include<>#include<>main(){float a,b,c;double x,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);}(1) #include<>main(){char c1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3); }(2) #include<>main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b); }(3) #include<>main(){int a,b;scanf("%2d%*2s%2d",&a,&b);printf("%d,%d\n",a,b);}#include<>main(){long a,b;float x,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);}#include<>main(){float a;printf("Innputa:");scanf("%f",&a);if(a>=0){a=a;printf("a=%f\n",a);}else{a=-a;printf("a=%f\n",a);}}#include<>main(){int a;printf("Inputa:");scanf("%d",&a);if(a%2==0){printf("a是偶数");}else{printf("a是奇数");}}#include<>#include<>main(){float a,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("不是三角形");}}#include<>#include<>{float a,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);}else if(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");}}#include<>main(){int year,flag;printf("Input a year:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0) {flag=1;}else{flag=0;}if(flag==1){printf("%d is a leap year !\n",year);}{printf("%d is not a leap year !\n",year);}}#include<>main(){int year,flag;printf("Input a year:");scanf("%d",&year);flag=year%400==0||year%4==0&&year%100!=01:0;if(flag==1&&flag!=0){printf("%d is a leap year !\n",year);}else{printf("%d is not a leap year !\n",year);}}#include<>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}else if(ch>='A'&&ch<='Z'){ch=getchar();ch=ch+32;printf("%c,%d\n",ch,ch);}else{printf("%c",ch);}}#include<>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch是数字字符\n");}else if(ch>=65&&ch<=90){printf("ch是大写字母\n");}else if(ch>=97&&ch<=122){printf("ch是小写字母\n");}else if(ch==32){printf("ch是空格\n");}else{printf("ch是其他字符\n");}}#include<>main(){int score,grade;printf("Input score:");scanf("%d",&score);grade=score/10;if(score<0||score>100){printf("Input error\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90){printf("%d--B\n",score);}else if(score>=70&&score<80){printf("%d--C\n",score);}else if(score>=60&&score<70){printf("%d--D\n",score);}else if(score>=0&&score<60){printf("%d- -E\n",score);}}#include<>main(){int year,month;printf("Input year,month:");scanf("%d,%d",&year,&month);if(month>12||month<=0){printf("error month\n");}else{switch(year,month){case 12:case 10:case 8:case 7:case 5:case 3:case 1:printf("31天\n");break;case 11:case 9:case 6:case 4:printf("30天\n");break;case 2:if(year%4==0&&year!=0||year%400==0){printf("29天\n");}else{printf("28天\n");}break;default:printf("Input error\n");}}}(1) #include<>main(){int i,j,k;char space=' ';for(i=1;i<=4;i++){for(j=1;j<=i;j++){printf("%c",space);}for(k=1;k<=6;k++){printf("*");}printf("\n");}}(2) #include<>main(){int k=4,n;for(n=0;n<k;n++){if(n%2==0) continue;k--;}printf("k=%d\n,n=%d\n",k,n);}(3) #include<>main(){int k=4,n;for(n=0;n<k;n++){if(n%2==0) break;k--;}printf("k=%d,n=%d\n",k,n);}(1) #include<>main(){int i,sum=0;for(i=1;i<=101;i++){sum=sum+i;}printf("sum=%d\n",sum);}(2) #include<>main(){long i;long term,sum=0;for(i=1;i<=101;i=i+2){term=i*(i+1)*(i+2);sum=sum+term;}printf("sum=%ld\n",sum);}(4) #include<>#include<>main(){int n=1;float term=,sign=1,sum=0;while(term<=-1e-4||term>=1e-4){term=sign;sum=sum+term;sign=sign+n;n++;}printf("sum=%f\n",sum);}(5) #include<>#include<>main(){int n=1,count=1;float x;double sum,term;printf("Input x:");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);}#include<>main(){int x=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++;}}}/* int x,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;}}}*/#include<>main(){int i,n;long p=1,m=1;printf("Input n:");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);}}#include<>main(){float c,f;for(c=-40;c<=110;c=c+10){f=9/5*c+32;printf("f=%f\n",f);}}#include<>#include<>main(){int n;double c=,x;do{x=x*pow(1+c,12)-1000;n++;}while(x>0);printf("x=%d\n",x);}#include<>main(){int n=0;float a=,c;printf("Inputc:");scanf("%f",&c);do{a=a*(1+c);n++;}while(a<=200);printf("n=%d\n",n);}#include<>#include<>main(){int n=1,count=1;double sum=1,term=1;while(fabs(term)>=1e-5){term=pow(-1,count)*(n+2));sum=sum+term;n=n+2;count++;}sum=4*sum;printf( "sum=%f,count=%d\n",sum,count); }#include<>#include<>main(){int n=1,count=1;double sum=1,term=1;while(fabs(term)>=1e-5){term=term*n);sum=sum+term;n++;count++;}printf("sum=%f,count=%d\n",sum,count);}#include<>#include<>main(){int x;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<>main(){int i=0,n;long sum=0,term=1;printf("Inputn:");scanf("%d",&n);do{i++;term=term*i;sum=sum+term;}while(sum<n);printf("%d\n",i-1);}#include<>main(){int i,n,m,count=0,sum=0;printf("Input n:");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);}}#include<>main(){int i,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);}}#include<>main(){int x,y,z;for(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);}}}#include<>main(){int x,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);}}}#include<>main(){int x,y,z;for(x=0;x<=20;x++){for(y=0;y<=33;y++){3*y+5*x+z/==100;z=100-x-y;if (5*x+3*y+z/==100&&z+x+y==100){printf("x=%d,y=%d,z=%d\n",x,y,z);}}}}#include<>main(){int x,y,z;for(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);}}}}#include<>int Square(int i){return i*i;}int main(){int i=0;i=Square(i);for( ;i<3;i++){static int i=1;i+=Square(i);printf("%d,",i);}printf("%d\n",i);return 0;}#include<>int hour,minute,second;void update(){second++;if(second==60){second=0;minute++;}if(minute==60){minute=0;hour++;}if(hour==24)hour=0;}void display(){printf("%d,%d,%d\n",hour,minute,second);}void delay(){int t;for(t=0;t<0;t++);}int main(){int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){update();display();delay();}return 0;}#include<>int GetMax(int a,int b);int main(){int x,y,max;printf("Inputx,y:");scanf("%d,%d",&x,&y);max=GetMax(x,y);printf("max=%d\n",max);return 0;}int GetMax(int m,int n){if(m>=n)return m;elsereturn n;}#include<>int LCM(int n,int m);int main(){int a,b;printf("Inputa,b:");scanf("%d,%d",&a,&b);printf("%d\n",LCM(a,b));return 0;}int LCM(int n,int m){int x;int find=0;for(x=1;!find;x++){if(x%n==0&&x%m==0){find=1;}}return x-1;}#include<>long Fact(int n);int main(){int m,a;printf("Inputm:");scanf("%d",&m);for(a=1;a<=m;a++){printf("%d!=%ld\n",a,Fact(a));}return 0;}long Fact(int n){int i;long result=1;for(i=2;i<=n;i++)result*=i;return result;}#include<>long Fact(int n);int main(){int m;long ret;printf("Inputm:");scanf("%d",&m);ret=Fact(m);printf("ret=%d\n",ret);return 0;}long Fact(int n){int i;long result=1,sum=0;for(i=2;i<=n;i++){result*=i;sum=sum+result;}return sum;}(1) #include<>int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return 0;}int Gcd(int a,int b){int t,min,find=0;min=a<ba:b;t=min;for(t=min;!find;t--){a%t==0;b%t==0;if(a%t==0&&b%t==0)return t;}find=1;}(2) #include<>int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return 0;}int Gcd(int a,int b){int r,temp;r=a%b;if(r==0)return b;elsedo{temp=b;b=r;a=temp;r=a%b;}while(r!=0);return b;}(1) #include<>void Func(int x){x=2;}int main(){int x=10;Func(x);printf("%d",x);return 0;}(2) #include<>void Func(int b[]){int j;for(j=0;j<4;j++){b[j]=j;}}int main(){static int a[]={5,6,7,8},i;Func(a);for(i=0;i<4;i++){printf("%d",a[i]);}return 0;}(1) int PositiveNum(int a[],int n){int i,count=0;for(i=0;i<n;i++){if(a[i]>0)count++;}return 0;}(2) void Fib(long f[],long n){int i;f[0]=0;f[1]=1;for(i=2;i<n;i++){f[i]=f[i-1]+f[i-2];}}(3) #include<>int main(){int a[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;}else if(a[n]<min){min=a[n];minPos=n;}}printf("max=%d,pos=%d\n",max,maxPos);printf("min=%d,pos=%d\n",min,minPos);return 0;}void DivArray(int *pArray,int n){int i;for(i=0;i<n;i++){pArray[i]/=pAttay[0];}}#include<>#define N 40int FailNum(int score[],int n);void ReadScore(int score[],int n);int main(){int score[N],n;printf("Input n:");scanf("%d",&n);ReadScore(score,n);FailNum(score,n);printf("FailNum students are %d\n",FailNum(score,n));return 0;}int FailNum(int score[],int n){int i,count=0;for(i=0;i<n;i++){if(score[i]<60)count++;}return count;}void ReadScore(int score[],int n){int i;printf("Input score:");for(i=0;i<n;i++){scanf("%d",&score[i]);}}#include<>#define N 40int HighAver(int score[],int n);int ReadScore(int score[]);int main(){int score[N],n;n=ReadScore(score);printf("HighAver students are %d\n",HighAver(score,n));return 0;}int HighAver(int score[],int n){int i,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++;}return count;}int ReadScore(int score[]){int i=-1;do{i++;printf("Input score:");scanf("%d",&score[i]);}while(score[i]>=0);return i;}#include<>#define N 40int ReadScore(int score[],long num[]);int FindMax(int score[],long num[],int n);int main(){int score[N],n,max,num,t;long num[N];t=FindMax(score,n);max=score[t];num=num[t];printf("max=%d,num=%d\n",max,num);return 0;}int ReadScore(int score[],long num[]){int i=-1;do{i++;printf("Input student's ID and score:");scanf("%ld%d",&num[i],&score[i]);}while(num[i]>0&&score[i]>=0);return i;}int FindMax(int score[],long num[],int n){int max,i;max=score[0];for(i=1;i<n;i++){if(score[i]>max)max=score[i];}return i;}#include<>#define N 40int Read(int a[]);int ChangeArry(int a[],int n);int main(){int a[N],n;printf("%d\n",ChangeArry(a,n));return 0;}int Read(int a[]){int i;for(i=0;i<9;i++){printf("Input a:");scanf("%d",&a[i]);return i;}int ChangeArry(int a[],int n){int max,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;}return a[i];}#include<>#define N 5void main(){int a[N][N];int sum = 0;int i, j;printf("请输入一个%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(i == j || i + j == N - 1)sum=sum+a[i][j];}}printf("对角线元素之和为:%d\n",sum);}#include<>#define N 2#define M 3void main(){int a[N][M],b[N][M];int i,j,sum1=0,sum2=0,sum=0;printf("请输入一个%d*%d的矩阵\n",N,M);for(i = 0; i<N; i++){for(j = 0; j< M; j++){scanf("%d",&a[i][j]);sum1=sum1+a[i][j];}}printf("请输入一个%d*%d的矩阵\n",N,M);for(i = 0; i <N; i++){for(j = 0; j <M; j++){scanf("%d",&b[i][j]);sum2=sum2+a[i][j];}}sum=sum1+sum2;printf("sum=%d\n",sum);}#include<>#define N 7void YangHui(int a[N][N],int n);void main(){int i,j,n,b[N][N];printf("Input n:");scanf("%d",&n);YangHui(b,n);for(i=0;i<n;i++){for(j=0;j<=i;j++){printf("%4d",b[i][j]);}printf("\n");}}void YangHui(int a[N][N],int n){int i,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];}}#include<>#define N 12void main(){int i;int f[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]);}}#include<>#include<>#include<>#define N 6000void main(){int a[N],i;int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0;float p1,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/;p2=(float)(b2/;p3=(float)(b3/;p4=(float)(b4/;p5=(float)(b5/;p6=(float)(b6/;}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);}(1) void Swap(int *x,int *y){int *pTemp;.\n");else if(strcmp(userInput,password)<0)printf("Invalid password!user input<password...\n");elseprintf("Invalid password!user input>password...\n");return 0;}#include<>#define N 24unsigned int CountLetter(char str[]);{char a[N];printf("Input a letter:\n");gets(a);printf("The length of the letter is:%d\n",CountLetter(a));return 0;}unsigned int CountLetter(char str[]){char *p=str;int c=0,flag=0;while(*p!='\0'){if(*p!=' ')flag=1;else if(flag==1){c++;flag=0;}p++;}return c+1;}#include<>typedef struct data{int year;int month;int day;}DATA;typedef struct work{char a[14];char b[10];char c[6];}WORK;typedef struct student{char studentName[10];char studentSex;DATA birthday;WORK profession;int main(){STUDENT stu={"王刚",'M',{1991,5,19},{"信息工程系","学习人员","学生"}};printf("stu:%2s%3c%6d/%02d/%02d%4s,%4s,%4s\n",,, return 0; }#include<>#include<>#define N 10typedef struct people{char peopleName[10];}PEOPLE;int main(){int sum1=0,sum2=0,sum3=0,sum4=0;int i;char name[N][N];PEOPLE stu[5]={"zhang","li","wang"};printf("Enter their name:\n");for(i=0;i<10;i++){gets(name[i]);if(strcmp(name[i],stu[0].peopleName)==0)sum1++;else if(strcmp(name[i],stu[1].peopleName)==0)sum2++;else if(strcmp(name[i],stu[2].peopleName)==0)sum3++;elsesum4++;}printf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);printf("stu:%4s%2d\n",stu[1].peopleName,sum2);printf("stu:%4s%2d\n",stu[2].peopleName,sum3);printf("%2d\n",sum4);return 0;}。

C语言程序设计苏小红版答案之欧阳育创编

C语言程序设计苏小红版答案之欧阳育创编

2.2 #include<stdio.h>main(){float x=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(){int a=12,b=3;float x=18.5,y=4.6;printf("%d\n",(float)(a*b)/2);printf("%d\n",(int)x%(int)y);}3.2 #include<stdio.h>main(){int x,b0,b1,b2,s;printf("Inputx:");scanf("%d",&x);b2=x/100;b1=(x-b2*100)/10;b0=x%10;s=b0*100+b1*10+b2;printf("s=%d\n",s);}3.3 #include<stdio.h>#include<math.h>main()float rate=0.0225;float n,capital,deposit;printf("Input n,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(){float a,b,c;double x,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);}4.1(1) #include<stdio.h> main(){char c1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3); }4.1(2) #include<stdio.h> main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b); }4.1(3) #include<stdio.h> main()int a,b;scanf("%2d%*2s%2d",&a,&b); printf("%d,%d\n",a,b);}4.2 #include<stdio.h> main(){long a,b;float x,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(){float a;printf("Innputa:");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()int a;printf("Inputa:");scanf("%d",&a);if(a%2==0){printf("a是偶数");}else{printf("a是奇数");}}5.3 #include<stdio.h>#include<math.h>main(){float a,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()float a,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);}else if(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(){int year,flag;printf("Input a year:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0) {flag=1;else{flag=0;}if(flag==1){printf("%d is a leap year !\n",year);}else{printf("%d is not a leap year !\n",year);}}5.6 #include<stdio.h>main(){int year,flag;printf("Input a year:");scanf("%d",&year);flag=year%400==0||year%4==0&&year%100!=0?1:0; if(flag==1&&flag!=0){printf("%d is a leap year !\n",year);}else{printf("%d is not a leap year !\n",year);}}5.7 #include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}else if(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(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch是数字字符\n");}else if(ch>=65&&ch<=90) {printf("ch是大写字母\n");}else if(ch>=97&&ch<=122)printf("ch是小写字母\n");}else if(ch==32){printf("ch是空格\n");}else{printf("ch是其他字符\n");}}5.9 #include<stdio.h>main(){int score,grade;printf("Input score:");scanf("%d",&score);grade=score/10;if(score<0||score>100){printf("Input error\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90) {printf("%d--B\n",score);}else if(score>=70&&score<80) {printf("%d--C\n",score);else if(score>=60&&score<70) {printf("%d--D\n",score);}else if(score>=0&&score<60){printf("%d- -E\n",score);}}5.10 #include<stdio.h>main(){int year,month;printf("Input year,month:");scanf("%d,%d",&year,&month);if(month>12||month<=0){printf("error month\n");}else{switch(year,month){case 12:case 10:case 8:case 7:case 5:case 3:case 1:printf("31天\n");break;case 11:case 9:case 6:case 4:printf("30天\n");break;case 2:if(year%4==0&&year!=0||year%400==0) {printf("29天\n");}else{printf("28天\n");}break;default:printf("Input error\n");}}}6.1(1) #include<stdio.h>main(){int i,j,k;char space=' ';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(){int k=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(){int k=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(){int i,sum=0;for(i=1;i<=101;i++){sum=sum+i;}printf("sum=%d\n",sum);}6.2(2) #include<stdio.h>main(){long i;long term,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(){int n=1;float term=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(){int n=1,count=1;double sum,term;printf("Input x:");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(){int x=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++;}}}/*int x,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);}}}*/6.4 #include<stdio.h>main(){int i,n;long p=1,m=1;printf("Input n:");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(){float c,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(){int n;double c=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(){int n=0;float a=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(){int n=1,count=1; double sum=1,term=1;。

c语言苏小红第五版第六章课后题答案

c语言苏小红第五版第六章课后题答案
}
else
{
if (magic4==1)
printf("No.please try again");
if (magic4==2)
printf("Wrong!try again");
if (magic4==3)
printf("Don't give up");
if (magic4==4)
printf("Not correct.Keep trying");
}
break;
}
rate=(float)count/10;
printf("%f\n",rate);
}
else
{
if (magic4==1)
printf("No.please try again");
if (magic4==2)
printf("Wrong!try again");
if (magic4==3)
printf("No.please try again");
if (magic4==2)
printf("Wrong!try again");
if (magic4==3)
printf("Don't give up");
if (magic4==4)
printf("Wrong!try again");
if (magic4==3)
printf("Don't give up");

高等教育出版社《C语言程序设计(苏小红)》课后习题答案

高等教育出版社《C语言程序设计(苏小红)》课后习题答案

2.2#include<stdio.h>main(){float x=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(){int a=12,b=3;float x=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(){int x=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(){int x,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(){float rate=0.0225;float n,capital,deposit;printf("Input n,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(){float a, b, c;double x, y;printf("Input a, 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(){char c1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3); }4.1(2)#include<stdio.h>main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b); }4.1(3)#include<stdio.h>main(){int a,b;scanf("%2d%*2s%2d",&a,&b);printf("%d,%d\n",a,b);}4.2#include<stdio.h>main(){long a,b;float x,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(){float a;printf("Input a:");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(){int a;printf("Input a:");scanf("%d", &a);if(a%2==0){printf("a是偶数");}else{printf("a是奇数");}}5.3#include<stdio.h>#include<math.h>main(){float a,b,c,s,area;printf("Input a, 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(){float a,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);}else if(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(){int year,flag;printf("Input a year:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0){flag=1;}else{flag=0;}if(flag==1){printf("%d is a leap year!\n",year);}else{printf("%d is not a leap year!\n",year);}}5.6#include<stdio.h>main(){int year,flag;printf("Input a year:");scanf("%d",&year);flag=year%400==0||year%4==0&&year%100!=0?1:0;if(flag==1&&flag!=0){printf("%d is a leap year!\n",year);}else{printf("%d is not a leap year!\n",year);}}5.7#include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}else if(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(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch是数字字符\n");}else if(ch>=65&&ch<=90){printf("ch是大写字母\n");}else if(ch>=97&&ch<=122){printf("ch是小写字母\n");}else if(ch==32){printf("ch是空格\n");}else{printf("ch是其他字符\n");}}5.9#include<stdio.h>main(){int score,grade;printf("Input score:");scanf("%d",&score);grade=score/10;if(score<0||score>100){printf("Input error\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90){printf("%d--B\n",score);}else if(score>=70&&score<80){printf("%d--C\n",score);}else if(score>=60&&score<70){printf("%d--D\n",score);}else if(score>=0&&score<60){printf("%d--E\n",score);}}5.10#include<stdio.h>main(){int year,month;printf("Input year,month:");scanf("%d,%d",&year,&month); if(month>12||month<=0){printf("error month\n");}else{switch(year,month){case12:case10:case8:case7:case5:case3:case1:printf("31天\n");break;case11:case9: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("Input error\n");}}}6.1(1)#include<stdio.h>main(){int i,j,k;char space='';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(){int k=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(){int k=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(){int i,sum=0;for(i=1;i<=101;i++){sum=sum+i;}printf("sum=%d\n",sum);}6.2(2)#include<stdio.h>main(){long i;long term,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(){int n=1;float term=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(){int n=1,count=1;float x;double sum,term;printf("Input x:");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(){int x=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++;}}}/*int x,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(){int i,n;long p=1,m=1;printf("Input n:");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(){float c,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(){int n;double c=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(){int n=0;float a=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(){int n=1,count=1;double sum=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(){int n=1,count=1;double sum=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>int x;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);}}6.11#include<stdio.h>main(){int i=0,n;long sum=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(){int i,n,m,count=0,sum=0;printf("Input n:");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(){int i,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(){int x,y,z;for(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(){int x,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(){int x,y,z;for(x=0;x<=20;x++){for(y=0;y<=33;y++){3*y+5*x+z/3.0==100;z=100-x-y;if(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(){int x,y,z;for(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>int Square(int i){return i*i;}int main(){int i=0;i=Square(i);for(;i<3;i++){static int i=1;i+=Square(i);printf("%d,",i);}printf("%d\n",i);return0;}7.2#include<stdio.h>int hour,minute,second;void update(){second++;if(second==60){second=0;minute++;}if(minute==60){minute=0;hour++;}if(hour==24)hour=0;}void display(){printf("%d,%d,%d\n",hour,minute,second);}void delay(){int t;for(t=0;t<100000000;t++); }int main(){int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){update();display();delay();}return0;}7.3#include<stdio.h>int GetMax(int a,int b);int main(){int x,y,max;printf("Inputx,y:");scanf("%d,%d",&x,&y);max=GetMax(x,y);printf("max=%d\n",max);return0;}int GetMax(int m,int n){if(m>=n)return m;elsereturn n;}7.4#include<stdio.h>int LCM(int n,int m);int main(){int a,b;printf("Inputa,b:");scanf("%d,%d",&a,&b);printf("%d\n",LCM(a,b));return0;}int LCM(int n,int m){int x;int find=0;for(x=1;!find;x++){if(x%n==0&&x%m==0){find=1;}}return x-1;}7.5#include<stdio.h>long Fact(int n);int main(){int m,a;printf("Inputm:");scanf("%d",&m);for(a=1;a<=m;a++){printf("%d!=%ld\n",a,Fact(a));}return0;}long Fact(int n){int i;long result=1;for(i=2;i<=n;i++)result*=i;return result;}7.6#include<stdio.h>long Fact(int n);int main(){int m;long ret;printf("Inputm:");scanf("%d",&m);ret=Fact(m);printf("ret=%d\n",ret);return0;}long Fact(int n){int i;long result=1,sum=0;for(i=2;i<=n;i++){result*=i;sum=sum+result;}return sum;}7.7(1)#include<stdio.h> int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}int Gcd(int a,int b){int t,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 t;}find=1;}7.7(2)#include<stdio.h> int Gcd(int a,int b);int main(){int m,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}int Gcd(int a,int b){int r,temp;r=a%b;if(r==0)return b;elsedo{temp=b;b=r;a=temp;r=a%b;}while(r!=0);return b;}8.1(1)#include<stdio.h> void Func(int x){x=2;}int main(){int x=10;Func(x);printf("%d",x);return0;}8.1(2)#include<stdio.h> void Func(int b[]){int j;for(j=0;j<4;j++){b[j]=j;}}int main(){static int a[]={5,6,7,8},i;Func(a);for(i=0;i<4;i++){printf("%d",a[i]);}return0;}8.2(1)int PositiveNum(int a[],int n) {int i,count=0;for(i=0;i<n;i++){if(a[i]>0)count++;}return0;}8.2(2)void Fib(long f[],long n){int i;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>int main(){int a[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;}else if(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.3void DivArray(int*pArray,int n){int i;for(i=0;i<n;i++){pArray[i]/=pAttay[0];}}8.4#include<stdio.h>#define N40int FailNum(int score[],int n);void ReadScore(int score[],int n);int main(){int score[N],n;printf("Input n:");scanf("%d",&n);ReadScore(score,n);FailNum(score,n);printf("FailNum students are%d\n",FailNum(score,n));return0;}int FailNum(int score[],int n){int i,count=0;for(i=0;i<n;i++){if(score[i]<60)count++;}return count;}void ReadScore(int score[],int n){int i;printf("Input score:");for(i=0;i<n;i++){scanf("%d",&score[i]);}}8.5#include<stdio.h>#define N40int HighAver(int score[],int n);int ReadScore(int score[]);int main(){int score[N],n;n=ReadScore(score);printf("HighAver students are%d\n",HighAver(score,n));return0;}int HighAver(int score[],int n){int i,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++;}return count;}int ReadScore(int score[]){int i=-1;do{i++;printf("Input score:");scanf("%d",&score[i]);}while(score[i]>=0);return i;}8.6#include<stdio.h>#define N40int ReadScore(int score[],long num[]);int FindMax(int score[],long num[],int n);int main(){int score[N],n,max,num,t;long num[N];t=FindMax(score,n);max=score[t];num=num[t];printf("max=%d,num=%d\n",max,num);return0;}int ReadScore(int score[],long num[]){int i=-1;do{i++;printf("Input student's ID and score:");scanf("%ld%d",&num[i],&score[i]);}while(num[i]>0&&score[i]>=0);return i;}int FindMax(int score[],long num[],int n) {int max,i;max=score[0];for(i=1;i<n;i++){if(score[i]>max)max=score[i];}return i;}8.7#include<stdio.h>#define N40int Read(int a[]);int ChangeArry(int a[],int n);int main(){int a[N],n;printf("%d\n",ChangeArry(a,n));return0;}int Read(int a[]){int i;for(i=0;i<9;i++){printf("Input a:");scanf("%d",&a[i]);return i;}int ChangeArry(int a[],int n){int max,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;}return a[i];}8.10#include<stdio.h>#define N5void main(){int a[N][N];int sum=0;int i,j;printf("请输入一个%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(i==j||i+j==N-1)sum=sum+a[i][j];}}printf("对角线元素之和为:%d\n",sum);}8.11#include<stdio.h>#define N2#define M3void main(){int a[N][M],b[N][M];int i,j,sum1=0,sum2=0,sum=0;printf("请输入一个%d*%d的矩阵\n",N,M);for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&a[i][j]);sum1=sum1+a[i][j];}}printf("请输入一个%d*%d的矩阵\n",N,M);for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&b[i][j]);sum2=sum2+a[i][j];}}sum=sum1+sum2;printf("sum=%d\n",sum);}8.12#include<stdio.h>#define N7void YangHui(int a[N][N],int n);void main(){int i,j,n,b[N][N];printf("Input n:");scanf("%d",&n);YangHui(b,n);for(i=0;i<n;i++){for(j=0;j<=i;j++){printf("%4d",b[i][j]);}printf("\n");}}void YangHui(int a[N][N],int n){int i,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>#define N12void main(){int i;int f[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>#define N6000void main(){int a[N],i;int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0;float p1,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)void Swap(int*x,int*y){int*pTemp;//*pTemp未进行初始化,*pTemp指向哪里位置*pTemp=*x;*x=*y;*y=*pTemp;}9.1(2)void Swap(int*x,int*y){int*pTemp;pTemp=x;x=y;y=pTemp;}//错误,x,y没有指向类型9.2#include<stdio.h>int main(){int a[]={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>void Swap(int*x,int*y);int main(){int a[6]={1,2,3,4,5,6},b[6]={11,22,33,44,55,66};int i;printf("Before Array\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("After Array\n");for(i=0;i<6;i++){printf("%d\n",a[i]);}for(i=0;i<6;i++){printf("%d\n",b[i]);}return0;}void Swap(int*x,int*y){int temp;temp=*x;*x=*y;*y=temp;}9.4#include<stdio.h>#define N10void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum); int main(){int a[N];int i,maxa,mina,maxnum,minnum;printf("Input a:");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;}void FindMaxMin(int a[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum) {int i;*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;}else if(a[i]<*pMina){*pMina=a[i];*pMinnum=i;}}}10.1void MYStrcpy(char*dstStr,const char*srcStr){while((*dstStr++=*srcStr++)!='\0'){}}10.2#include<stdio.h>main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);} 10.3(1)unsigned int MyStrlen(char*p)main(){unsigned int len;len=0;for(;*pa!='\0';pa++){len++;}return len;}10.3(2)unsigned int MyStrlen(char s[]){char*p=s;while(*p!='\0'){p++;}return(p-*p);}10.3(3)int MyStrcmp(char*p1,char*p2){for(;*p1==*p2;p1++,p2++){if(*p1=='\0')return-1;}return1;}10.3(4)int MyStrcmp(char s[],char t[]){int i;for(i=0;s[i]==t[i];i++){if(s[i]=='\0')return0;}return i;}10.3(5)#include<stdio.h>#include<string.h>int main(){char password[7]="secret";char userInput[81];printf("Input Password:");scanf("%s",userInput);if(strcmp(userInput,password)==0)printf("Coorrect password!Welcome to the system...\n");else if(strcmp(userInput,password)<0)printf("Invalid password!user input<password...\n");elseprintf("Invalid password!user input>password...\n");return0;}10.4#include<stdio.h>#define N24unsigned int CountLetter(char str[]);int main(){char a[N];printf("Input a letter:\n");gets(a);printf("The length of the letter is:%d\n",CountLetter(a));return0;}unsigned int CountLetter(char str[]){char*p=str;int c=0,flag=0;while(*p!='\0'){if(*p!='')flag=1;else if(flag==1){c++;flag=0;}p++;}return c+1;}12.1#include<stdio.h>typedef struct data{int year;int month;int day;}DATA;typedef struct work{char a[14];char b[10];char c[6];}WORK;typedef struct student{char studentName[10];char studentSex;DATA birthday;WORK profession;}STUDENT;int main(){STUDENT stu={"王刚",'M',{1991,5,19},{"信息工程系","学习人员","学生"}};printf("stu:%2s%3c%6d/%02d/%02d%4s,%4s,%4s\n",stu.studentName,stu.stud entSex,stu.birthday.year,stu.birthday.month,stu.birthday.day,stu.profession.a,stu.profession.b,stu.profession.c);return0;}12.4#include<stdio.h>#include<string.h>#define N10typedef struct people{char peopleName[10];}PEOPLE;int main(){int sum1=0,sum2=0,sum3=0,sum4=0;int i;char name[N][N];PEOPLE stu[5]={"zhang","li","wang"};printf("Enter their name:\n");for(i=0;i<10;i++){gets(name[i]);if(strcmp(name[i],stu[0].peopleName)==0)sum1++;else if(strcmp(name[i],stu[1].peopleName)==0) sum2++;else if(strcmp(name[i],stu[2].peopleName)==0) sum3++;elsesum4++;}printf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);printf("stu:%4s%2d\n",stu[1].peopleName,sum2);printf("stu:%4s%2d\n",stu[2].peopleName,sum3);printf("%2d\n",sum4);return0;}。

C语言程序设计苏小红版答案之欧阳索引创编

C语言程序设计苏小红版答案之欧阳索引创编

2.2 #include<stdio.h>欧阳家百(2021.03.07)main(){float x=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(){int a=12,b=3;float x=18.5,y=4.6;printf("%d\n",(float)(a*b)/2);printf("%d\n",(int)x%(int)y);}3.2 #include<stdio.h>main(){int x,b0,b1,b2,s;printf("Inputx:");scanf("%d",&x);b2=x/100;b1=(x-b2*100)/10;b0=x%10;s=b0*100+b1*10+b2;printf("s=%d\n",s);}3.3 #include<stdio.h>#include<math.h>main(){float rate=0.0225;float n,capital,deposit;printf("Input n,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(){double x,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);}4.1(1) #include<stdio.h>main(){char c1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3); }4.1(2) #include<stdio.h>main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b); }4.1(3) #include<stdio.h>main(){scanf("%2d%*2s%2d",&a,&b); printf("%d,%d\n",a,b);}4.2 #include<stdio.h>main(){long a,b;float x,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(){float a;printf("Innputa:");scanf("%f",&a);if(a>=0){a=a;}else{a=-a;printf("a=%f\n",a);}}5.2 #include<stdio.h> main(){int a;printf("Inputa:"); scanf("%d",&a);if(a%2==0){printf("a是偶数");}else{printf("a是奇数");}}#include<math.h>main(){float a,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(){float a,b,c,x,y;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);}else if(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");}}{int year,flag;printf("Input a year:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0) {flag=1;}else{flag=0;}if(flag==1){printf("%d is a leap year !\n",year);}else{printf("%d is not a leap year !\n",year);}}{int year,flag;printf("Input a year:");scanf("%d",&year);flag=year%400==0||year%4==0&&year%100!=0?1:0; if(flag==1&&flag!=0){printf("%d is a leap year !\n",year);}else{printf("%d is not a leap year !\n",year);}}5.7 #include<stdio.h>main(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z')ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}else if(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(){char ch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57)printf("ch是数字字符\n");}else if(ch>=65&&ch<=90) {printf("ch是大写字母\n");}else if(ch>=97&&ch<=122) {printf("ch是小写字母\n");}else if(ch==32){printf("ch是空格\n");}else{printf("ch是其他字符\n");}}5.9 #include<stdio.h>main(){printf("Input score:");scanf("%d",&score);grade=score/10;if(score<0||score>100){printf("Input error\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}else if(score>=80&&score<90) {printf("%d--B\n",score);}else if(score>=70&&score<80) {printf("%d--C\n",score);}else if(score>=60&&score<70) {printf("%d--D\n",score);else if(score>=0&&score<60) {printf("%d- -E\n",score);}}5.10 #include<stdio.h>main(){int year,month;printf("Input year,month:"); scanf("%d,%d",&year,&month); if(month>12||month<=0){printf("error month\n");}else{switch(year,month){case 12:case 10:case 8:case 5:case 3:case 1:printf("31天\n");break;case 11:case 9:case 6:case 4:printf("30天\n");break;case 2:if(year%4==0&&year!=0||year%400==0) {printf("29天\n");}else{printf("28天\n");}break;default:}}6.1(1) #include<stdio.h> main(){int i,j,k;char space=' ';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>int k=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(){int k=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()int i,sum=0;for(i=1;i<=101;i++){sum=sum+i;}printf("sum=%d\n",sum); }6.2(2) #include<stdio.h> main(){long i;long term,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()int n=1;float term=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(){int n=1,count=1;float x;double sum,term;printf("Input x:");scanf("%f",&x);sum=x;term=x;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(){int x=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++;}}}/*int x,find=0;{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(){int i,n;long p=1,m=1;printf("Input n:");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);}}main(){float c,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(){int n;double c=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>{int n=0;float a=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(){int n=1,count=1;double sum=1,term=1;while(fabs(term)>=1e-5){term=pow(-1,count)*(1.0/(n+2)); sum=sum+term;count++;}sum=4*sum;printf("sum=%f,count=%d\n",sum,count); }6.9 #include<stdio.h>#include<math.h>main(){int n=1,count=1;double sum=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>{int x;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);}}6.11 #include<stdio.h>main(){int i=0,n;long sum=0,term=1;printf("Inputn:");scanf("%d",&n);do{i++;term=term*i;sum=sum+term;}while(sum<n);printf("%d\n",i-1);}main(){int i,n,m,count=0,sum=0;printf("Input n:");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>{int i,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()int x,y,z;for(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(){int x,y;for(x=0;x<=98;x++){y=98-x;2*x+4*y==386;if(x+y==98&&2*x+4*y==386){}}}6.16 #include<stdio.h>main(){int x,y,z;for(x=0;x<=20;x++){for(y=0;y<=33;y++){3*y+5*x+z/3.0==100;z=100-x-y;if (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()int x,y,z;for(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>int Square(int i){return i*i;}int main(){int i=0;for( ;i<3;i++){static int i=1;i+=Square(i);printf("%d,",i);}printf("%d\n",i); return 0;}7.2 #include<stdio.h> int hour,minute,second; void update(){second++;if(second==60){second=0;minute++;}if(minute==60){minute=0;}if(hour==24)hour=0;}void display(){printf("%d,%d,%d\n",hour,minute,second); }void delay(){int t;for(t=0;t<100000000;t++);}int main(){int i;void updaye(),display(),delay();for(i=0;i<1000000;i++){update();display();delay();return 0;}7.3 #include<stdio.h> int GetMax(int a,int b); int main(){int x,y,max;printf("Inputx,y:"); scanf("%d,%d",&x,&y); max=GetMax(x,y); printf("max=%d\n",max); return 0;}int GetMax(int m,int n) {if(m>=n)return m;elsereturn n;}7.4 #include<stdio.h> int LCM(int n,int m);int a,b;printf("Inputa,b:"); scanf("%d,%d",&a,&b); printf("%d\n",LCM(a,b)); return 0;}int LCM(int n,int m){int x;int find=0;for(x=1;!find;x++){if(x%n==0&&x%m==0) {find=1;}}return x-1;}7.5 #include<stdio.h> long Fact(int n);int m,a;printf("Inputm:");scanf("%d",&m);for(a=1;a<=m;a++){printf("%d!=%ld\n",a,Fact(a)); }return 0;}long Fact(int n){int i;long result=1;for(i=2;i<=n;i++)result*=i;return result;}7.6 #include<stdio.h>long Fact(int n);int main(){long ret;printf("Inputm:");scanf("%d",&m);ret=Fact(m);printf("ret=%d\n",ret); return 0;}long Fact(int n){int i;long result=1,sum=0;for(i=2;i<=n;i++){result*=i;sum=sum+result;}return sum;}7.7(1) #include<stdio.h> int Gcd(int a,int b);int main(){printf("Inputm,n:"); scanf("%d,%d",&m,&n); printf("%d\n",Gcd(m,n)); return 0;}int Gcd(int a,int b){int t,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 t;}find=1;}7.7(2) #include<stdio.h> int Gcd(int a,int b);int main()int m,n;printf("Inputm,n:"); scanf("%d,%d",&m,&n); printf("%d\n",Gcd(m,n)); return 0;}int Gcd(int a,int b){int r,temp;r=a%b;if(r==0)return b;elsedo{temp=b;b=r;a=temp;r=a%b;}while(r!=0);return b;}8.1(1) #include<stdio.h>{x=2;}int main(){int x=10;Func(x);printf("%d",x);return 0;}8.1(2) #include<stdio.h> void Func(int b[]){int j;for(j=0;j<4;j++){b[j]=j;}}int main(){static int a[]={5,6,7,8},i;for(i=0;i<4;i++){printf("%d",a[i]);}return 0;}8.2(1) int PositiveNum(int a[],int n) {int i,count=0;for(i=0;i<n;i++){if(a[i]>0)count++;}return 0;}8.2(2) void Fib(long f[],long n){int i;f[0]=0;f[1]=1;for(i=2;i<n;i++){}}8.2(3) #include<stdio.h>int main(){int a[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;}else if(a[n]<min){min=a[n];}}printf("max=%d,pos=%d\n",max,maxPos); printf("min=%d,pos=%d\n",min,minPos); return 0;}8.3 void DivArray(int *pArray,int n){int i;for(i=0;i<n;i++){pArray[i]/=pAttay[0];}}8.4 #include<stdio.h>#define N 40int FailNum(int score[],int n);void ReadScore(int score[],int n);int main(){int score[N],n;printf("Input n:");scanf("%d",&n);ReadScore(score,n);FailNum(score,n);printf("FailNum students are %d\n",FailNum(score,n)); return 0;}int FailNum(int score[],int n){int i,count=0;for(i=0;i<n;i++){if(score[i]<60)count++;}return count;}void ReadScore(int score[],int n){int i;printf("Input score:");for(i=0;i<n;i++){scanf("%d",&score[i]);}}8.5 #include<stdio.h>#define N 40int HighAver(int score[],int n);int ReadScore(int score[]);int main(){int score[N],n;n=ReadScore(score);printf("HighAver students are %d\n",HighAver(score,n)); return 0;}int HighAver(int score[],int n){int i,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++;}return count;}int ReadScore(int score[]){int i=-1;do{i++;printf("Input score:");scanf("%d",&score[i]);}while(score[i]>=0);return i;}8.6 #include<stdio.h>#define N 40int ReadScore(int score[],long num[]); int FindMax(int score[],long num[],int n); int main(){int score[N],n,max,num,t;long num[N];t=FindMax(score,n);max=score[t];num=num[t];printf("max=%d,num=%d\n",max,num); return 0;}int ReadScore(int score[],long num[]){int i=-1;do{i++;printf("Input student's ID and score:"); scanf("%ld%d",&num[i],&score[i]);}while(num[i]>0&&score[i]>=0); return i;}int FindMax(int score[],long num[],int n) {int max,i;max=score[0];for(i=1;i<n;i++){if(score[i]>max)max=score[i];return i;}8.7 #include<stdio.h>#define N 40int Read(int a[]);int ChangeArry(int a[],int n);int main(){int a[N],n;printf("%d\n",ChangeArry(a,n)); return 0;}int Read(int a[]){int i;for(i=0;i<9;i++){printf("Input a:");scanf("%d",&a[i]);return i;}int ChangeArry(int a[],int n)int max,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;}return a[i];}8.10 #include<stdio.h>#define N 5void main(){int a[N][N];int sum = 0;int i, j;printf("请输入一个%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(i == j || i + j == N - 1)sum=sum+a[i][j];}}printf("对角线元素之和为:%d\n",sum); }8.11 #include<stdio.h>#define N 2#define M 3void main()int a[N][M],b[N][M];int i,j,sum1=0,sum2=0,sum=0;printf("请输入一个%d*%d的矩阵\n",N,M); for(i = 0; i<N; i++){for(j = 0; j< M; j++){scanf("%d",&a[i][j]);sum1=sum1+a[i][j];}}printf("请输入一个%d*%d的矩阵\n",N,M); for(i = 0; i <N; i++){for(j = 0; j <M; j++){scanf("%d",&b[i][j]);sum2=sum2+a[i][j];}}sum=sum1+sum2;printf("sum=%d\n",sum);8.12 #include<stdio.h>#define N 7void YangHui(int a[N][N],int n); void main(){int i,j,n,b[N][N];printf("Input n:");scanf("%d",&n);YangHui(b,n);for(i=0;i<n;i++){for(j=0;j<=i;j++){printf("%4d",b[i][j]);}printf("\n");}}void YangHui(int a[N][N],int n) {int i,j;for(i=0;i<n;i++)。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档