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语言程序设计苏小红版答案之欧阳文创编
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;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); 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;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){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(){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); 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++){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];minPos=n;}}printf("max=%d,pos=%d\n",max,maxPos); printf("min=%d,pos=%d\n",min,minPos);}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));}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);。
第三版苏小红c语言答案C语言程序设计苏小红版答案
第三版苏小红c语言答案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是奇数");}11。
苏小红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语言程序设计苏小红版答案之欧阳道创编
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;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;。
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语言程序设计》(苏小红)-课后习题答案-高等教育出版社
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语言程序设计》(苏小红) 课后习题答案 高等教育出版社资料
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语言概述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语言程序设计》(苏小红) 课后习题参考答案 高等教育出版社
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语言概述1. 问题:C语言的特点是什么?答案:C语言的特点包括简洁高效、结构化、可移植性高、丰富的运算符、数据类型丰富等。
2. 问题:C语言的发展历程是怎样的?答案:C语言起源于20世纪70年代,由丹尼斯·里奇(Dennis Ritchie)在贝尔实验室开发,最初用于UNIX操作系统的编写。
随着UNIX的流行,C语言也逐渐被广泛使用。
第二章:数据类型、运算符和表达式1. 问题:C语言中的基本数据类型有哪些?答案:C语言中的基本数据类型包括整型(int)、字符型(char)、浮点型(float和double)等。
2. 问题:算术运算符有哪些?答案:算术运算符包括加(+)、减(-)、乘(*)、除(/)和模运算(%)。
第三章:控制语句1. 问题:if语句的基本形式是什么?答案:if语句的基本形式是:```cif (条件) {// 条件为真时执行的代码} else {// 条件为假时执行的代码}```2. 问题:for循环的一般形式是什么?答案:for循环的一般形式是:```cfor (初始化表达式; 条件表达式; 迭代表达式) {// 循环体}```第四章:数组1. 问题:一维数组的声明方式是什么?答案:一维数组的声明方式是:```c类型数组名[数组大小];```2. 问题:如何使用数组?答案:使用数组时,可以通过数组名和索引来访问数组元素,例如: ```cint arr[10];arr[0] = 1; // 给数组的第一个元素赋值```第五章:函数1. 问题:函数的定义包括哪些部分?答案:函数的定义包括返回类型、函数名、参数列表和函数体。
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<>{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是偶数");}elseprintf("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<>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");}}#include<>main(){int year,flag;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);}}#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);}elseprintf("%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));}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);}(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[]);int main(){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;}STUDENT;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语言概述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语言程序设计苏小红版答案之欧阳育创编
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语言程序设计》(苏小红) 课后习题参考答案高等教育出版社《C语言程序设计》(苏小红) 课后习题参考答案一、基础知识题1. C语言是一种通用的、面向过程的程序设计语言。
它在20世纪70年代由贝尔实验室的Dennis M. Ritchie开发。
2. C程序的基本组成部分有:预处理指令、函数、变量和语句。
3. C语言的数据类型包括:整型、浮点型、字符型、枚举型和指针型等。
4. C语言的运算符包括:算术运算符、关系运算符、逻辑运算符和位运算符等。
5. C语言的控制语句包括:顺序结构、分支结构和循环结构等。
6. C语言的函数由函数头和函数体组成,其中函数头包括函数的返回类型、函数名和参数列表。
7. C语言的数组是一种存储相同类型数据元素的连续存储区域。
8. C语言中的字符串是由字符组成的字符数组。
二、程序设计题1. 编写一个程序,输入一个整数n,计算并输出1到n之间的所有正整数的平方和。
```c#include<stdio.h>int main() {int n;int sum = 0;printf("请输入一个整数n:");scanf("%d", &n);for (int i = 1; i <= n; i++) {sum += i * i;}printf("1到%d之间的所有正整数的平方和为:%d\n", n, sum);return 0;}```2. 编写一个程序,输入一个正整数n,判断该数是否为素数(只能被1和自身整除)。
```c#include<stdio.h>int isPrime(int n) {if (n <= 1)return 0;for (int i = 2; i <= n / 2; i++) {if (n % i == 0)return 0;}return 1;}int main() {int n;printf("请输入一个正整数n:"); scanf("%d", &n);if (isPrime(n))printf("%d是素数\n", n);elseprintf("%d不是素数\n", n);return 0;}```3. 编写一个程序,输入一个正整数n,输出n的阶乘。
《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>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语言概述1. 问题:C语言的特点是什么?答案:C语言具有简洁、紧凑、灵活、高效等特点。
它是一种结构化语言,支持多种数据类型,并且具有丰富的运算符。
C语言的程序设计语言简洁,语法结构清晰,易于学习。
2. 问题:C语言的主要应用领域有哪些?答案:C语言广泛应用于系统软件、应用软件、嵌入式系统、操作系统、数据库管理系统等领域。
第二章:数据类型、运算符和表达式1. 问题:C语言中的基本数据类型有哪些?答案:C语言的基本数据类型包括整型(int)、字符型(char)、浮点型(float和double)等。
2. 问题:如何理解C语言中的运算符优先级?答案:C语言中的运算符优先级决定了表达式中各个运算符的执行顺序。
例如,乘除运算符的优先级高于加减运算符,括号内的运算优先级最高。
第三章:控制结构1. 问题:C语言中的三种基本控制结构是什么?答案:C语言中的三种基本控制结构是顺序结构、选择结构和循环结构。
2. 问题:if语句的基本形式有哪些?答案:if语句的基本形式包括单分支if语句、双分支if-else语句和多分支if-else if-else语句。
第四章:数组1. 问题:什么是一维数组?答案:一维数组是具有相同数据类型元素的集合,这些元素在内存中连续存储,可以通过索引访问。
2. 问题:如何声明和初始化一个一维数组?答案:声明一维数组的语法是 `type arrayName[arraySize];`。
初始化可以使用 `{value1, value2, ...}` 的方式,例如 `intarr[5] = {1, 2, 3, 4, 5};`。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#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);
for(c=-40;c<=110;c=c+10)
{
f=9/5*c+32;
printf("f=%f\n",f);
}
}
#include<>
}
#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);
}
}
#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')
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);
}
(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);
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);
}
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:");
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++;
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);
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");
{
flag=0;
}
if(flag==1)
{
printf("%d is a leap year !\n",year);
}
else
{
printf("%d is not a leap year !\n",year);
}
}
#include<>
main()
{
int year,flag;
printf("Input a year:");
{
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);
}
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);
{
printf("x=%d\n",x);
find=1;
x+&nt 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()
{
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);
}
(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);