c语言第十一章(苏小红版)

合集下载

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

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

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

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

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

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

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

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

include<>main{float x=,y=,z=;printf"x=%f\n",x;printf"y=%f\n",y;printf"z=%f\n",z;}1include<>main{int a=12,b=3;float x=,y=;printf"%f\n",floatab/2;printf"%d\n",intx%inty;}2include<>main{int x=32,y=81,p,q;p=x++;q=--y;printf"%d %d\n",p,q;printf"%d %d\n",x,y;}include<>main{int x,b0,b1,b2,s;printf"Inputx:";scanf"%d",&x;b2=x/100;b1=x-b2100/10;f\n",p1;printf"骰子出现2的概率:printf"骰子出现3的概率:printf"骰子出现4的概率:printf"骰子出现5的概率:printf"骰子出现6的概率:}1void Swapint x,int y{int pTemp;.\n";else ifstrcmpuserInput,password<0printf"Invalid passworduser input<password...\n";elseprintf"Invalid passworduser input>password...\n";return0;}include<>define N24unsigned int CountLetterchar str;int main{char aN;printf"Input a letter:\n";getsa;printf"The length of the letter is:%d\n",CountLettera;return0;}unsigned int CountLetterchar str{char p=str;int c=0,flag=0;whilep='\0'{ifp=''flag=1;else ifflag==1{c++;flag=0;}p++;}return c+1;}include<>typedef struct data{int year;int month;int day;}DATA;typedef struct work{char a14;char b10;char c6;}WORK;typedef struct student{char studentName10;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",,,,0;}include<>include<>define N10typedef struct people{char peopleName10;}PEOPLE;int main{int sum1=0,sum2=0,sum3=0,sum4=0;int i;char nameNN;PEOPLE stu5={"zhang","li","wang"};printf"Enter their name:\n";fori=0;i<10;i++{getsnamei;ifstrcmpnamei,stu0.peopleName==0sum1++;else ifstrcmpnamei,stu1.peopleName==0sum2++;else ifstrcmpnamei,stu2.peopleName==0sum3++;elsesum4++;}printf"stu:%4s%2d\n",stui.peopleName,sumj;printf"stu:%4s%2d\n",stu1.peopleName,sum2;printf"stu:%4s%2d\n",stu2.peopleName,sum3;printf"%2d\n",sum4;return0;}。

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

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

2・ 2 #include<stdio. h>main (){f loat x=2・ 5, y二2・ 5, z=2・ 5;printf("x=%f\n",x);printf (z,y=%f\n,,> y);printf("z=%f\n",z);}3. 1(1) #include<stdio. h>main (){int a二12, b二3;f loat 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, bO, bl, b2, s;printf (/z Input x: 〃);b2=x/100;bl=(x-b2*100)/10;b0=x%10;s=b0*100+bl*10+b2;printf("s=%d\n", s);}3.3 ftinclude<stdio. h>ftinclude<math. h>main (){float rate=0. 0225;float n, capital, deposit;pri ntf (“ Input n, cap it al:z,):scanf ("%f, %f", &n, &capital);deposit二capital*pow(1+rate, n);printf(〃deposit=%f\n", deposit); }3・ 4 #include<stdio. h>ftinclude<math・ h>main ()double x, y;printf ("Inputa, b, c:"); scanf("%f, %f, %f", &a, &b, &c);x= (-b+sqrt(b*b-4*a*c))/(2*a);y= (-b-sqrt(b*b-4*a*c))/(2*a); printfy=%f\n,z, x, y);}4.1 (1) #include<stdio. h>main (){char cl二'a' , c2二'b' , c3二'c';printf ("a%cb%cc%c\n", cl, c2, c3);}4.1 (2) #include<stdio. h>main (){int a=12, b=15;4. 1 (3) ttinclude<stdio. h>printf (,,a=%d%%, b=%d%%\n,z, a, b);}main ()4. 1 (3) ttinclude<stdio. h>int a, b;scanf(〃%2d%*2s%2d〃,&a, &b);printf ("%d, %d\n", a, b);}4・ 2 #include<stdio・ h>main (){long a, b;f loat x, y;scanf ("%d, %d\n", &a, &b);scanf (〃%f, %f\n", &x, &y);printf ("a=%d, b二%d\n", a, b); printfb=%f\n,z, x, y);}5・ 1 #include<stdio. h>main (){float a;printf ("Irmputa: 〃);scanf ("%f", &a);if(a>=0)a 二a;printf \n〃, a);}elseprintf Ca=%f\n \ a);}}5・ 2 #include<stdio. h>main (){int a;printf (/z Inputa:;scanf &a);if(a%2==0){printffa 是偶数“);}elseprintf (〃a 是奇数〃);}5・ 3 #include<stdio. h>#include<math・ h> main (){float a, b, c, s, area;printf("Inputa, b,c:");scanf (”%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 (z,area=%f\n/z, area);}elseprintf C不是三角形〃);}}5・ 4 #include<stdio. h> ftinclude<math・ h>main (){f loat a, b, c, x, y;printf ("Inputa, b, c:");scanf (“%f, %f, %f", &a, &b, &c);if(a==0){printfC该方程不是一元二次方程\『);}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 }printfC该方程无实根\n〃);}5. 5 #include<stdio. h>main (){int year,flag;printf("Input a year:");scanf&year);if (year%4==0&&year%400!=0 |year%400==0) {flag=l;}else{flag=O;}if(flag==l){printf("%d is a leap year !\n", year);}elseprintf (z,%d is not a leap year ! \n \ year):}}5・ 6 #include<stdio. h>main (){int year,flag;printf("Input a year:");scanf&year);f lag=year%400~0 丨year%4==0&&year%100! =0?l: 0;if(flag==l&&flag!=O){printf (z,%d is a leap year ! \n,z, year);}else{printf (z,%d is not a leap year ! \n z,, year):}}5・ 7 nclude〈stdio. h> }main (){char ch;printf("Inputch:“);scanf&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{}printfch); }5.8 #include〈stdio. h> main (){char ch;printf("Inputch:“);scanf &ch);if(ch>=48&&ch<=57){printf ("ch是数字字符\n");}else if(ch>=65&&ch<=90){printf (,z ch 是大写字母\n");}else if(ch>=97&&ch<=122){printf (〃ch是小写字母\n〃);}else if(ch==32)printf ("ch 是空格\rT);else}printf ("ch是其他字符\rT);}}5・ 9 #include<stdio. h>main (){int score, grade;printf("Input score:");scanf(“%d", &score);grade二score, 10;if (score<0 Iscore>100){printf ("Input error\n,z):}if (score>=90&&score〈二100){printf ("%d-一A\n,z, 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:,z);scanf %d,z, &year, &month);辻(month>12 month<=0) printf ("30 天\n〃);printf (z,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:break;case 2:辻(year%4==0&&year!=0 year%400==0){printf ("29 天\n〃);}else{printf ("28 天\n");}break;defauIt:printf ("Input error\n,/);}}}6. 1 (1) ttinclude<stdio. h>main (){int i, j, k;char space二'';for(i=l;i<=4;i++)printf ("30 天\n〃);printf(“%c", space);}for (k=l;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;6.1 (3) #include<stdio. h> main ()printf ("k=%d\n, n=%d\n", k, n);{int k=4, n;for (n二0;n〈k;n++){if(n%2二二0) break;}printf n二%d\n", k, n); }6. 2 (1) #include<stdio. h>main (){int i, sum=0:for(i=l;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=l;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> ftinclude<math・ h> main (){int n二1;float term =1.0, sign二1,sum二0;while(term〈二-le-4||term>=le-4) {term二1. 0, sign;sum 二sum+term;sign^sign+n;n++;printf("sum二%f\n", sum);}6. 2 (5) #include<stdio. h> ftinclude<math・ h>main (){int n=l, count=l;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++;jwhile(fabs(term)>=le-5);printf(〃sin(x)=%f, count二%d\n" \ sum, count);{ 6. 3 #include<stdio. h>main (){int x=l, find=O:while(!find){辻(x%2==l&&x%3==2&&x%5==4&&x%6==5&&x%7==0){printf ("x=%d\n", x);find=l;x++;}}}/* int x,find 二0;for (x=l;!find;x++){辻(x%2 二二 1&&X %3 二二 2&&x%5=4&&x%6 二二 5&&x%7==0)printf ("x=%d\n",x);find=l;{}{ }}*/6.48include<stdio. h>main (){int i, n;long p=l, m=l;printf("Input n:");scanf&n);for(i=l;i<=n;i++){p二i*i;m二i*i*i;printf ("p=%d, nF%d\n", i, p, i, m);}}6.5#include<stdio. h>main ()f loat c, f;for(c=-40;c<=110;c=c+10){ f二9/5*c+32; printf (/,f zz%f\n,/, f);6.6#include<stdio. h> ftinclude<math. h>main ()int n;double c=0. 01875, x;do {x=x*pow(l+c, 12)-1000;n++;}while(x>0);printf (,,x=%d\n,,> x);6.7nclude〈stdio. h> main ()int n二0;printf (,z 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二l,count二1;double sum二1, term二1;while(fabs(term)>=le-5){term=pow(-l, 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>ftinclude<math. h>main (){int n=l, count=l;double sum=l, term^l;while (fabs(term)>=le-5){term二term*(1. 0/n);sum二sum+term;n++;count++;}printf(〃sum=%f, count二%d\r/ \ sum, count);}6.10#include<stdio. h>ftinclude<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 nclude<stdio. h> main (){int i=0, n;long sum=0, term=l; printf("Inputn:"); scanf&n);do {i++;term二term*i;sum=sum+term;:while(sum<n);printf("%d\n", i~l);main (){int i, n, m, count=0, sum=O;printf("Input n:");scanf&n);for (i=l;i<=n;i++){printf ("Inputm:");scanf&m);if(m>0){sum二sum+m;count++;}else{break;}printf ("sum二%d, count二%d\n,z, sum, count);}main (){int i, n, m, count=0, sum=O;printf("Inputn:”);scanf&n);for(i=l;i<=n;i++){printf("Inputm:“);scanf &m);辻(m>0 m<0){sum=sum+m;count++;}else{break;}printf("sum二%d, count二%d\n", sum, count); }main (){int x, y, z;for (x=O;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;辻(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;i.f (5*x+3*y+z/3. 0二二100&&z+x+y二二100){printf ("x二%d, y=%d, z二%d\n", x, y, z);}6.17ftinclude<stdio. h>main (){int x, y, z;for (x=l;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 nclude〈stdio. h>int Square(int i)return i*i; int Square(int 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\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 nclude〈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)}int GetMax(int m, int n)if(m>二n)return m;elsereturn n;}7・ 4 nclude〈stdio. h>int LCM(int n, int m);int main (){int a, b;printf (,z 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=l;!find;x++)if(x%n==0&&x%m==0)find=l;}}return x-1:}7・ 5 #include〈stdio. h>long Fact(int n);int main (){int m, a;printf("Inputm:");scanf &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 resuIt;7.6 #include<stdio. h> long Fact(int n); int mainOint m;long ret;printf("Inputm:");scanf&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=O;min=a<b?a:b;t二min;for(t二min;!find;t一一)a%t==O;b%t==O;if(a%t==O&&b%t==O) return t;}find=l;}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;。

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

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

2.2 #include<stdio.h>main(){float x=2.5,y=2.5,z=2.5;printf("x=%f\n",x);printf("y=%f\n",y);printf("z=%f\n",z);}3.1(1) #include<stdio.h>main(){int a=12,b=3;float x=18.5,y=4.6;printf("%d\n",(float)(a*b)/2);printf("%d\n",(int)x%(int)y);}3.2 #include<stdio.h>main(){int x,b0,b1,b2,s;printf("Inputx:");scanf("%d",&x);b2=x/100;b1=(x-b2*100)/10;b0=x%10;s=b0*100+b1*10+b2;printf("s=%d\n",s);}3.3 #include<stdio.h>#include<math.h>main(){float rate=0.0225;float n,capital,deposit;printf("Input n,capital:");scanf("%f,%f",&n,&capital);deposit=capital*pow(1+rate,n);printf("deposit=%f\n",deposit); }3.4 #include<stdio.h>#include<math.h>main(){float a,b,c;double x,y;printf("Inputa,b,c:");scanf("%f,%f,%f",&a,&b,&c);x=(-b+sqrt(b*b-4*a*c))/(2*a);y=(-b-sqrt(b*b-4*a*c))/(2*a);printf("x=%f,y=%f\n",x,y);}4.1(1) #include<stdio.h>main(){char c1='a',c2='b',c3='c';printf("a%cb%cc%c\n",c1,c2,c3); }4.1(2) #include<stdio.h>main(){int a=12,b=15;printf("a=%d%%,b=%d%%\n",a,b); }4.1(3) #include<stdio.h>main(){int a,b;scanf("%2d%*2s%2d",&a,&b);printf("%d,%d\n",a,b);}4.2 #include<stdio.h>main(){long a,b;float x,y;scanf("%d,%d\n",&a,&b);scanf("%f,%f\n",&x,&y);printf("a=%d,b=%d\n",a,b);printf("x=%f,b=%f\n",x,y);}5.1 #include<stdio.h>main(){float a;printf("Innputa:");scanf("%f",&a);if(a>=0){a=a;printf("a=%f\n",a);}else{a=-a;printf("a=%f\n",a);}}5.2 #include<stdio.h>main(){int a;printf("Inputa:");scanf("%d",&a);if(a%2==0){printf("a是偶数");}else{printf("a是奇数");}}5.3 #include<stdio.h>#include<math.h>main(){float a,b,c,s,area;printf("Inputa,b,c:");scanf("%f,%f,%f",&a,&b,&c);if(a+b>c&&a+c>b&&b+c>a){s=(a+b+c)/2;area=(float)sqrt(s*(s-a)*(s-b)*(s-c));printf("area=%f\n",area);}else{printf("不是三角形");}}5.4 #include<stdio.h>#include<math.h>{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);}{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);return 0;}8.3 void DivArray(int *pArray,int n){int i;for(i=0;i<n;i++){pArray[i]/=pAttay[0];}}8.4 #include<stdio.h>#define N 40int FailNum(int score[],int n);void ReadScore(int score[],int n);int main(){int score[N],n;printf("Input n:");scanf("%d",&n);ReadScore(score,n);FailNum(score,n);printf("FailNum students are %d\n",FailNum(score,n));return 0;}int FailNum(int score[],int n){int i,count=0;for(i=0;i<n;i++){if(score[i]<60)count++;}return count;}void ReadScore(int score[],int n){int i;printf("Input score:");for(i=0;i<n;i++){scanf("%d",&score[i]);}}8.5 #include<stdio.h>#define N 40int HighAver(int score[],int n);int ReadScore(int score[]);int main(){int score[N],n;n=ReadScore(score);printf("HighAver students are %d\n",HighAver(score,n));return 0;}int HighAver(int score[],int n){int i,count=0,sum=0,aver;for(i=0;i<n;i++){sum=sum+score[i];aver=sum/n;}for(i=0;i<n;i++){if(score[i]>aver)count++;}return count;}int ReadScore(int score[]){int i=-1;do{i++;printf("Input score:");scanf("%d",&score[i]);}while(score[i]>=0);return i;}8.6 #include<stdio.h>#define N 40int ReadScore(int score[],long num[]);int FindMax(int score[],long num[],int n);int main(){int score[N],n,max,num,t;long num[N];t=FindMax(score,n);max=score[t];num=num[t];printf("max=%d,num=%d\n",max,num);return 0;}int ReadScore(int score[],long num[]){int i=-1;do{i++;printf("Input student's ID and score:");scanf("%ld%d",&num[i],&score[i]);}while(num[i]>0&&score[i]>=0);return i;}int FindMax(int score[],long num[],int n){int max,i;max=score[0];for(i=1;i<n;i++){if(score[i]>max)max=score[i];}return i;}8.7 #include<stdio.h>#define N 40int Read(int a[]);int ChangeArry(int a[],int n);int main(){int a[N],n;printf("%d\n",ChangeArry(a,n));return 0;}int Read(int a[]){int i;for(i=0;i<9;i++){printf("Input a:");scanf("%d",&a[i]);return i;}int ChangeArry(int a[],int n){int max,min,i,maxpos,minpos,term;max=min=score[0];for(i=1;i<n;i++){if(a[i]>max)max=score[i];maxpos=n;else(a[i]<min)min=a[i];minpos=n;temp=maxpos;maxpos=minpos;minpos=temp;}return a[i];}8.10 #include<stdio.h>#define N 5void main(){int a[N][N];int sum = 0;int i, j;printf("请输入一个%d*%d的矩阵\n",N,N);for(i = 0; i < N; i++){for(j = 0; j < N; j++){scanf("%d",&a[i][j]);}}for(i = 0; i < N; i++){for(j = 0; j < N; j++){if(i == j || i + j == N - 1)sum=sum+a[i][j];}}printf("对角线元素之和为:%d\n",sum);}8.11 #include<stdio.h>#define N 2#define M 3void main(){int a[N][M],b[N][M];int i,j,sum1=0,sum2=0,sum=0;printf("请输入一个%d*%d的矩阵\n",N,M);for(i = 0; i<N; i++){for(j = 0; j< M; j++){scanf("%d",&a[i][j]);sum1=sum1+a[i][j];}}printf("请输入一个%d*%d的矩阵\n",N,M);for(i = 0; i <N; i++){for(j = 0; j <M; j++){scanf("%d",&b[i][j]);sum2=sum2+a[i][j];}}sum=sum1+sum2;printf("sum=%d\n",sum);}8.12 #include<stdio.h>#define N 7void YangHui(int a[N][N],int n);void main(){int i,j,n,b[N][N];printf("Input n:");scanf("%d",&n);YangHui(b,n);for(i=0;i<n;i++){for(j=0;j<=i;j++){printf("%4d",b[i][j]);}printf("\n");}}void YangHui(int a[N][N],int n){int i,j;for(i=0;i<n;i++){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 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]);}}8.14 #include<stdio.h>#include<stdlib.h>#include<time.h>#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/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);return 0;}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]);}return 0;}void Swap(int *x,int *y){int temp;temp=*x;*x=*y;*y=temp;}9.4 #include<stdio.h>#define N 10void 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);return 0;}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.1 void 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;}return 1;}10.3(4) int MyStrcmp(char s[],char t[]){int i;for(i=0;s[i]==t[i];i++){if(s[i]=='\0') return 0;}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");return 0;}10.4 #include<stdio.h>#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;}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,stu.birt hday.year,stu.birthday.month,stu.birthday.day,stu.profession.a,stu.profession.b,stu.profession.c);return 0;}12.4 #include<stdio.h>#include<string.h>#define N 10typedef struct people{char peopleName[10];}PEOPLE;int main(){int sum1=0,sum2=0,sum3=0,sum4=0;int i;char name[N][N];PEOPLE stu[5]={"zhang","li","wang"};printf("Enter their name:\n");for(i=0;i<10;i++){gets(name[i]);if(strcmp(name[i],stu[0].peopleName)==0)sum1++;else if(strcmp(name[i],stu[1].peopleName)==0) sum2++;else if(strcmp(name[i],stu[2].peopleName)==0) sum3++;elsesum4++;}printf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);printf("stu:%4s%2d\n",stu[1].peopleName,sum2);printf("stu:%4s%2d\n",stu[2].peopleName,sum3);printf("%2d\n",sum4);return 0;}。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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;printf("s=%d\n",s);}3.3#include<stdio.h>#include<math.h>main(){float rate=0.0225;float n,capital,deposit;printf("Inputn,capital:");scanf("%f,%f",&n,&capital);deposit=capital*pow(1+rate,n);printf("deposit=%f\n",deposit);}3.4#include<stdio.h>#include<math.h>main(){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);}习题 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(){longa,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("Inputa:");scanf("%f",&a);if(a>=0){a=a;printf("a=%f\n",a);}else{a=-a;printf("a=%f\n",a);}}5.2#include<stdio.h>main(){inta;printf("Inputa:");scanf("%d",&a);if(a%2==0){printf("a 是偶数 ");}else{printf("a 是奇数 ");}}5.3#include<stdio.h>#include<math.h>main(){floata,b,c,s,area;printf("Inputa,b,c:");scanf("%f%f%f",&a,&b,&c);if(a+b>c&&a+c>b&&b+c>a){s=(a+b+c)/2;area=(float)sqrt(s*(s-a)*(s-b)*(s-c));printf("area=%f\n",area);}else{printf(" 不是三角形 ");}}5.4#include<stdio.h>#include<math.h>main(){floata,b,c,x,y;printf("Inputa,b,c:");scanf("%f,%f,%f",&a,&b,&c);if(a==0){printf(" 该方程不是一元二次方程\n");}if(b*b-4*a*c>0){x=(-b+sqrt(b*b-4*a*c))/(2*a);y=(-b-sqrt(b*b-4*a*c))/(2*a);printf("x=%f,y=%f\n",x,y);}elseif(b*b-4*a*c==0){x=-b/(2*a);y=-b/(2*a);printf("x=%f,y=%f\n",x,y);}else{printf(" 该方程无实根 \n");}}5.5#include<stdio.h>main(){intyear,flag;printf("Inputayear:");scanf("%d",&year);if(year%4==0&&year%400!=0||year%400==0){flag=1;}else{flag=0;}if(flag==1){printf("%disaleapyear!\n",year);}else{printf("%disnotaleapyear!\n",year);}}5.6#include<stdio.h>main(){intyear,flag;printf("Inputayear:");scanf("%d",&year);flag=year%400==0||year%4==0&&year%100!=0?1:0;if(flag==1&&flag!=0){printf("%disaleapyear!\n",year);}else{printf("%disnotaleapyear!\n",year);}}5.7#include<stdio.h>main(){charch;printf("Inputch:");scanf("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;printf("%c,%d\n",ch,ch);}elseif(ch>='A'&&ch<='Z'){ch=getchar();ch=ch+32;printf("%c,%d\n",ch,ch);}else{printf("%c",ch);}}5.8#include<stdio.h>main(){charch;printf("Inputch:");scanf("%c",&ch);if(ch>=48&&ch<=57){printf("ch 是数字字符 \n");}elseif(ch>=65&&ch<=90){printf("ch 是大写字母 \n");}elseif(ch>=97&&ch<=122){printf("ch 是小写字母 \n");}elseif(ch==32){printf("ch 是空格 \n");}else{printf("ch 是其他字符 \n");}}5.9#include<stdio.h>main(){intscore,grade;printf("Inputscore:");scanf("%d",&score);grade=score/10;if(score<0||score>100){printf("Inputerror\n");}if(score>=90&&score<=100){printf("%d--A\n",score);}elseif(score>=80&&score<90)printf("%d--B\n",score);}elseif(score>=70&&score<80){printf("%d--C\n",score);}elseif(score>=60&&score<70){printf("%d--D\n",score);}elseif(score>=0&&score<60){printf("%d--E\n",score);}}5.10#include<stdio.h>main(){intyear,month;printf("Inputyear,month:");scanf("%d,%d",&year,&month);if(month>12||month<=0){printf("errormonth\n");}else{switch(year,month){case12:case10:case8:case7:case5:case3:case1: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("Inputerror\n");}}}6.1(1)#include<stdio.h>main(){inti,j,k;charspace='';for(i=1;i<=4;i++){for(j=1;j<=i;j++){printf("%c",space);}for(k=1;k<=6;k++){printf("*");}printf("\n");}}6.1(2)#include<stdio.h>main(){intk=4,n;for(n=0;n<k;n++){if(n%2==0)continue;k--;}printf("k=%d\n,n=%d\n",k,n);}6.1(3)#include<stdio.h>main(){intk=4,n;for(n=0;n<k;n++){if(n%2==0)break;k--;}printf("k=%d,n=%d\n",k,n);}6.2(1)#include<stdio.h>main(){inti,sum=0;for(i=1;i<=101;i++){sum=sum+i;}printf("sum=%d\n",sum);}6.2(2)#include<stdio.h>main(){longi;longterm,sum=0;for(i=1;i<=101;i=i+2){term=i*(i+1)*(i+2);sum=sum+term;}printf("sum=%ld\n",sum);}6.2(4)#include<stdio.h>#include<math.h>main(){intn=1;floatterm=1.0,sign=1,sum=0;while(term<=-1e-4||term>=1e-4){term=1.0/sign;sum=sum+term;sign=sign+n;n++;}printf("sum=%f\n",sum);}6.2(5)#include<stdio.h>#include<math.h>main(){intn=1,count=1;floatx;doublesum,term;printf("Inputx:");scanf("%f",&x);sum=x;term=x;do{term=-term*x*x/((n+1)*(n+2));sum=sum+term;n=n+2;count++;}while(fabs(term)>=1e-5);printf("sin(x)=%f,count=%d\n",sum,count);}6.3#include<stdio.h>main(){intx=1,find=0;while(!find){if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0){printf("x=%d\n",x);find=1;x++;}}}/*intx,find=0;for(x=1;!find;x++){if(x%2==1&&x%3==2&&x%5==4&&x%6==5&&x%7==0){printf("x=%d\n",x);find=1;}}}*/6.4#include<stdio.h>main(){inti,n;longp=1,m=1;printf("Inputn:");scanf("%d",&n);for(i=1;i<=n;i++){p=i*i;m=i*i*i;printf("p=%d,m=%d\n",i,p,i,m);}}6.5#include<stdio.h>main(){floatc,f;for(c=-40;c<=110;c=c+10){f=9/5*c+32;printf("f=%f\n",f);}}6.6#include<stdio.h>#include<math.h>main(){intn;doublec=0.01875,x;do{x=x*pow(1+c,12)-1000;n++;}while(x>0);printf("x=%d\n",x);}6.7#include<stdio.h>main(){intn=0;floata=100.0,c;printf("Inputc:");scanf("%f",&c);do{a=a*(1+c);n++;}while(a<=200);printf("n=%d\n",n);}6.8#include<stdio.h>#include<math.h>main(){intn=1,count=1;doublesum=1,term=1;while(fabs(term)>=1e-5){term=pow(-1,count)*(1.0/(n+2));sum=sum+term;n=n+2;count++;}sum=4*sum;printf("sum=%f,count=%d\n",sum,count); }6.9#include<stdio.h>#include<math.h>main(){intn=1,count=1;doublesum=1,term=1;while(fabs(term)>=1e-5){term=term*(1.0/n);sum=sum+term;n++;count++;}printf("sum=%f,count=%d\n",sum,count); }6.10#include<stdio.h>#include<math.h>main(){intx;for(x=100;x<=999;x++){if(x==pow(x/100,3)+pow(x/10%10,3)+pow(x%10,3))printf("x=%d\n",x);}}6.11#include<stdio.h>main(){inti=0,n;longsum=0,term=1;printf("Inputn:");scanf("%d",&n);do{i++;term=term*i;sum=sum+term;}while(sum<n);printf("%d\n",i-1);}6.12#include<stdio.h>main(){inti,n,m,count=0,sum=0;printf("Inputn:");scanf("%d",&n);for(i=1;i<=n;i++){printf("Inputm:");scanf("%d",&m);if(m>0){sum=sum+m;count++;}else{break;}printf("sum=%d,count=%d\n",sum,count);}}6.13#include<stdio.h>main(){inti,n,m,count=0,sum=0;printf("Inputn:");scanf("%d",&n);for(i=1;i<=n;i++){printf("Inputm:");scanf("%d",&m);if(m>0||m<0){sum=sum+m;count++;}else{break;}printf("sum=%d,count=%d\n",sum,count);}}6.14#include<stdio.h>main(){intx,y,z;for(x=0;x<=17;x++){for(y=0;y<=25;y++){3*x+2*y+z==50;z=30-x-y;if(3*x+2*y+z==50&&x+y+z==30)printf("x=%d,y=%d,z=%d\n",x,y,z);}}}6.15#include<stdio.h>main(){intx,y;for(x=0;x<=98;x++){y=98-x;2*x+4*y==386;if(x+y==98&&2*x+4*y==386){printf("x=%d,y=%d\n",x,y);}}}6.16#include<stdio.h>main(){intx,y,z;for(x=0;x<=20;x++){for(y=0;y<=33;y++){3*y+5*x+z/3.0==100;z=100-x-y;if(5*x+3*y+z/3.0==100&&z+x+y==100){printf("x=%d,y=%d,z=%d\n",x,y,z);}}}}6.17#include<stdio.h>main(){intx,y,z;for(x=1;x<=9;x++){for(y=1;y<=17;y++){10*x+5*y+z==100;z=50-x-y;if(10*x+5*y+z==100&&x+y+z==50&&z>0){printf("x=%d,y=%d,z=%d\n",x,y,z);}}}}7.1#include<stdio.h>intSquare(inti){returni*i;}intmain(){inti=0;i=Square(i);for(;i<3;i++){staticinti=1;i+=Square(i);printf("%d,",i);}printf("%d\n",i);return0;}7.2#include<stdio.h>inthour,minute,second;voidupdate(){second++;if(second==60){second=0;minute++;}if(minute==60){minute=0;hour++;}if(hour==24)hour=0;}voiddisplay(){printf("%d,%d,%d\n",hour,minute,second);}voiddelay(){intt;for(t=0;t<100000000;t++);}intmain(){inti;voidupdaye(),display(),delay();for(i=0;i<1000000;i++){update();display();delay();}return0;}7.3#include<stdio.h>intGetMax(inta,intb);intmain(){intx,y,max;printf("Inputx,y:");scanf("%d,%d",&x,&y);max=GetMax(x,y);printf("max=%d\n",max);return0;}intGetMax(intm,intn){if(m>=n)returnm;elsereturnn;}7.4#include<stdio.h>intLCM(intn,intm);intmain(){inta,b;printf("Inputa,b:");scanf("%d,%d",&a,&b);printf("%d\n",LCM(a,b));return0;}intLCM(intn,intm){intx;intfind=0;for(x=1;!find;x++){if(x%n==0&&x%m==0){find=1;}}returnx-1;}7.5#include<stdio.h>longFact(intn);intmain(){intm,a;printf("Inputm:");scanf("%d",&m);for(a=1;a<=m;a++){printf("%d!=%ld\n",a,Fact(a));}return0;}longFact(intn){inti;longresult=1;for(i=2;i<=n;i++)result*=i;returnresult;}7.6#include<stdio.h>longFact(intn);intmain(){intm;longret;printf("Inputm:");scanf("%d",&m);ret=Fact(m);printf("ret=%d\n",ret);return0;}longFact(intn){inti;longresult=1,sum=0;for(i=2;i<=n;i++){result*=i;sum=sum+result;}returnsum;}7.7(1)#include<stdio.h> intGcd(inta,intb);intmain(){intm,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}intGcd(inta,intb){intt,min,find=0;min=a<b?a:b;t=min;for(t=min;!find;t--){a%t==0;b%t==0;if(a%t==0&&b%t==0)returnt;}find=1;}7.7(2)#include<stdio.h> intGcd(inta,intb);intmain(){intm,n;printf("Inputm,n:");scanf("%d,%d",&m,&n);printf("%d\n",Gcd(m,n));return0;}intGcd(inta,intb){intr,temp;r=a%b;if(r==0)returnb;elsedo{temp=b;b=r;a=temp;r=a%b;}while(r!=0);returnb;}8.1(1)#include<stdio.h> voidFunc(intx){x=2;}intmain(){intx=10;Func(x);printf("%d",x);return0;}8.1(2)#include<stdio.h> voidFunc(intb[]){intj;for(j=0;j<4;j++){b[j]=j;}}intmain(){staticinta[]={5,6,7,8},i;Func(a);for(i=0;i<4;i++){printf("%d",a[i]);}return0;}8.2(1)intPositiveNum(inta[],intn){inti,count=0;for(i=0;i<n;i++){if(a[i]>0)count++;}return0;}8.2(2)voidFib(longf[],longn){inti;f[0]=0;f[1]=1;for(i=2;i<n;i++){f[i]=f[i-1]+f[i-2];}}8.2(3)#include<stdio.h>intmain(){inta[10],n,max,min,maxPos,minPos;for(n=0;n<10;n++){scanf("%d",&a[n]);}max=min=a[0];maxPos=minPos=0;for(n=0;n<10;n++){if(a[n]>max){max=a[n];maxPos=n;}elseif(a[n]<min){min=a[n];minPos=n;}}printf("max=%d,pos=%d\n",max,maxPos);printf("min=%d,pos=%d\n",min,minPos);return0;}8.3voidDivArray(int*pArray,intn){inti;for(i=0;i<n;i++){pArray[i]/=pAttay[0];}}8.4#include<stdio.h>#defineN40intFailNum(intscore[],intn);voidReadScore(intscore[],intn);intmain(){intscore[N],n;printf("Inputn:");scanf("%d",&n);ReadScore(score,n);FailNum(score,n);printf("FailNumstudentsare%d\n",FailNum(score,n));return0;}intFailNum(intscore[],intn){inti,count=0;for(i=0;i<n;i++){if(score[i]<60)count++;}returncount;}voidReadScore(intscore[],intn){inti;printf("Inputscore:");for(i=0;i<n;i++){scanf("%d",&score[i]);}}8.5#include<stdio.h>#defineN40intHighAver(intscore[],intn);intReadScore(intscore[]);intmain(){intscore[N],n;n=ReadScore(score);printf("HighAverstudentsare%d\n",HighAver(score,n));return0;}intHighAver(intscore[],intn){inti,count=0,sum=0,aver;for(i=0;i<n;i++){sum=sum+score[i];aver=sum/n;}for(i=0;i<n;i++){if(score[i]>aver)count++;}returncount;}intReadScore(intscore[]){inti=-1;do{i++;printf("Inputscore:");scanf("%d",&score[i]);}while(score[i]>=0);returni;}8.6#include<stdio.h>#defineN40intReadScore(intscore[],longnum[]);intFindMax(intscore[],longnum[],intn);intmain(){intscore[N],n,max,num,t;longnum[N];t=FindMax(score,n);max=score[t];num=num[t];printf("max=%d,num=%d\n",max,num);return0;}intReadScore(intscore[],longnum[]){inti=-1;do{i++;printf("Inputstudent'sIDandscore:");scanf("%ld%d",&num[i],&score[i]);}while(num[i]>0&&score[i]>=0);returni;}intFindMax(intscore[],longnum[],intn){intmax,i;max=score[0];for(i=1;i<n;i++){if(score[i]>max)max=score[i];}returni;}8.7#include<stdio.h>#defineN40intRead(inta[]);intChangeArry(inta[],intn);intmain(){inta[N],n;printf("%d\n",ChangeArry(a,n));return0;}intRead(inta[]){inti;for(i=0;i<9;i++){printf("Inputa:");scanf("%d",&a[i]);returni;}intChangeArry(inta[],intn){intmax,min,i,maxpos,minpos,term;max=min=score[0];for(i=1;i<n;i++){if(a[i]>max)max=score[i];maxpos=n;else(a[i]<min)min=a[i];minpos=n;temp=maxpos;maxpos=minpos;minpos=temp;}returna[i];}8.10#include<stdio.h>#defineN5voidmain(){inta[N][N];intsum=0;inti,j;printf(" 请输入一个 %d*%d 的矩阵 \n",N,N);for(i=0;i<N;i++){for(j=0;j<N;j++){scanf("%d",&a[i][j]);}}for(i=0;i<N;i++){for(j=0;j<N;j++){if(i==j||i+j==N-1)sum=sum+a[i][j];}}printf(" 对角线元素之和为: %d\n",sum);}8.11#include<stdio.h>#defineN2#defineM3voidmain(){inta[N][M],b[N][M];inti,j,sum1=0,sum2=0,sum=0;printf(" 请输入一个 %d*%d 的矩阵 \n",N,M);for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&a[i][j]);sum1=sum1+a[i][j];}}printf(" 请输入一个 %d*%d 的矩阵 \n",N,M);for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&b[i][j]);sum2=sum2+a[i][j];}}sum=sum1+sum2;printf("sum=%d\n",sum);}8.12#include<stdio.h>#defineN7voidYangHui(inta[N][N],intn);voidmain(){inti,j,n,b[N][N];printf("Inputn:");scanf("%d",&n);YangHui(b,n);for(i=0;i<n;i++){for(j=0;j<=i;j++){printf("%4d",b[i][j]);}printf("\n");}}voidYangHui(inta[N][N],intn){inti,j;for(i=0;i<n;i++){a[i][0]=1;a[i][i]=1;}for(i=2;i<n;i++){for(j=1;j<i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];}}8.13#include<stdio.h>#defineN12voidmain(){inti;intf[N]={1,1};printf("%d:%d\n",0,f[0]);printf("%d:%d\n",1,f[1]);for(i=2;i<N;i++){f[i]=f[i-1]+f[i-2];printf("%d:%d\n",i+1,f[i]);}}8.14#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN6000voidmain(){inta[N],i;intb1=0,b2=0,b3=0,b4=0,b5=0,b6=0;floatp1,p2,p3,p4,p5,p6;srand(time(NULL));for(i=0;i<N;i++){a[i]=rand()%6+1;printf("%3d",a[i]);if(a[i]==1)b1++;if(a[i]==2)b2++;if(a[i]==3)b3++;if(a[i]==4)b4++;if(a[i]==5)b5++;if(a[i]==6)b6++;p1=(float)(b1/6000.0);p2=(float)(b2/6000.0);p3=(float)(b3/6000.0);p4=(float)(b4/6000.0);p5=(float)(b5/6000.0);p6=(float)(b6/6000.0);}printf(" 骰子出现 1 的概率: %.2f\n",p1);printf(" 骰子出现 2 的概率: %.2f\n",p2);printf(" 骰子出现 3 的概率: %.2f\n",p3);printf(" 骰子出现 4 的概率: %.2f\n",p4);printf(" 骰子出现 5 的概率: %.2f\n",p5);printf(" 骰子出现 6 的概率: %.2f\n",p6);}9.1(1)voidSwap(int*x,int*y){int*pTemp;//*pTemp 未进行初始化, *pTemp 指向哪里位置*pTemp=*x;*x=*y;*y=*pTemp;}9.1(2)voidSwap(int*x,int*y){int*pTemp;pTemp=x;x=y;y=pTemp;}//错误, x,y 没有指向类型9.2#include<stdio.h>intmain(){inta[]={1,2,3,4,5};int*p=a;printf("%d,%d,%d,%d,%d,%d,%d\n",*p,*(++p),(*p)++,*p,*p--,--(*p),*p);return0;}9.3#include<stdio.h>voidSwap(int*x,int*y);intmain(){inta[6]={1,2,3,4,5,6},b[6]={11,22,33,44,55,66};inti;printf("BeforeArray\n");for(i=0;i<6;i++){printf("%d\n",a[i]);}for(i=0;i<6;i++){printf("%d\n",b[i]);}for(i=0;i<6;i++){Swap(&a[i],&b[i]);}printf("AfterArray\n");for(i=0;i<6;i++){printf("%d\n",a[i]);}for(i=0;i<6;i++){printf("%d\n",b[i]);}return0;}voidSwap(int*x,int*y){inttemp;temp=*x;*x=*y;*y=temp;}9.4#include<stdio.h>#defineN10voidFindMaxMin(inta[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum);intmain(){inta[N];inti,maxa,mina,maxnum,minnum;printf("Inputa:");for(i=0;i<10;i++){scanf("%d",&a[i]);}FindMaxMin(a,&maxa,&maxnum,&mina,&minnum);printf("maxa=%d,maxnum=%d,mina=%d,minnum=%d\n",maxa,maxnum,mina,minnum);return0;}voidFindMaxMin(inta[],int*pMaxa,int*pMaxnum,int*pMina,int*pMinnum){inti;*pMaxa=a[0];*pMina=a[0];*pMaxnum=0;*pMinnum=0;for(i=0;i<10;i++){if(a[i]>*pMaxa){*pMaxa=a[i];*pMaxnum=i;}elseif(a[i]<*pMina){*pMina=a[i];*pMinnum=i;}}}10.1voidMYStrcpy(char*dstStr,constchar*srcStr){while((*dstStr++=*srcStr++)!='\0'){}}10.2#include<stdio.h>main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);} 10.3(1)unsignedintMyStrlen(char*p)main(){unsignedintlen;len=0;for(;*pa!='\0';pa++){len++;}returnlen;}10.3(2)unsignedintMyStrlen(chars[]){char*p=s;while(*p!='\0'){p++;}return(p-*p);}10.3(3)intMyStrcmp(char*p1,char*p2){for(;*p1==*p2;p1++,p2++){if(*p1=='\0')return-1;}return1;}10.3(4)intMyStrcmp(chars[],chart[]){inti;for(i=0;s[i]==t[i];i++){if(s[i]=='\0')return0;}returni;}10.3(5)#include<stdio.h>#include<string.h>intmain(){charpassword[7]="secret";charuserInput[81];printf("InputPassword:");scanf("%s",userInput);if(strcmp(userInput,password)==0)printf("Coorrectpassword!Welcometothesystem...\n");elseif(strcmp(userInput,password)<0)printf("Invalidpassword!userinput<password...\n");elseprintf("Invalidpassword!userinput>password...\n");return0;}10.4#include<stdio.h>#defineN24unsignedintCountLetter(charstr[]);intmain(){chara[N];printf("Inputaletter:\n");gets(a);printf("Thelengthoftheletteris:%d\n",CountLetter(a));return0;}unsignedintCountLetter(charstr[]){char*p=str;intc=0,flag=0;while(*p!='\0'){if(*p!='')flag=1;elseif(flag==1){c++;flag=0;}p++;}returnc+1;}12.1#include<stdio.h>typedefstructdata{intyear;intmonth;intday;}DATA;typedefstructwork{chara[14];charb[10];charc[6];}WORK;typedefstructstudent{charstudentName[10];charstudentSex;DATAbirthday;WORKprofession;}STUDENT;intmain(){STUDENTstu={" 王刚 ",'M',{1991,5,19},{" 信息工程系 ","学习人员 ","学生 "}};printf("stu:%2s%3c%6d/%02d/%02d%4s,%4s,%4s\n",stu.studentName,stu.studentSex,st u.birthday.year,stu.birthday.month,stu.birthday.day,stu.profession.a,stu.profession.b,stu.profession.c);return0;}12.4#include<stdio.h>#include<string.h>#defineN10typedefstructpeople{charpeopleName[10];}PEOPLE;intmain(){intsum1=0,sum2=0,sum3=0,sum4=0;inti;charname[N][N];PEOPLEstu[5]={"zhang","li","wang"};printf("Entertheirname:\n");for(i=0;i<10;i++){if(strcmp(name[i],stu[0].peopleName)==0)sum1++;elseif(strcmp(name[i],stu[1].peopleName)==0) sum2++;elseif(strcmp(name[i],stu[2].peopleName)==0) sum3++;elsesum4++;}printf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);printf("stu:%4s%2d\n",stu[1].peopleName,sum2);printf("stu:%4s%2d\n",stu[2].peopleName,sum3);printf("%2d\n",sum4);return0;}。

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

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

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

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

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

2.2 #i nclude<stdio.h>main (){float x=2.5,y=2.5,z=2.5;prin tf("x=%f\n",x);prin tf("y=%f\n",y);prin tf("z=%f\n",z);}3.1(1) #in clude<stdio.h>main (){int a=12,b=3;float x=18.5,y=4.6;prin tf("%d\n",(float)(a*b)/2); printf("%d\n",(i nt)x%(i nt)y);}3.2 #i nclude<stdio.h>main (){int x,b0,b1,b2,s;prin tf("I nputx:");scan f("%d",& x);b2=x/100;b仁(x-b2*100)/10;b0=x%10; s=b0*100+b1*10+b2;prin tf("s=%d\n",s);}3.3 #in clude<stdio.h>#in clude<math.h>main (){float rate=0.0225;float n, capital,deposit;prin tf("I nput n, capital:"); scanf("%f,%f",&n,& capital);deposit=capital*pow(1+rate ,n);prin tf("deposit=%f\n",deposit);}3.4 #in clude<stdio.h>#in clude<math.h>main ()float a,b,c;double x,y;prin tf("l nputa,b,c:");scan f("%f,%f,%f",&a,&b, &c);x=(-b+sqrt(b*b-4*a*c))/(2*a);y=(-b-sqrt(b*b-4*a*c))/(2*a);prin tf("x=%f,y=%f\n",x,y);}4.1(1) #i nclude<stdio.h>main (){char c1='a',c2='b',c3='c';prin tf("a%cb%cc%c\n",c1,c2,c3);}4.1(2) #i nclude<stdio.h>main (){int a=12,b=15;prin tf("a=%d%%,b=%d%%\n",a,b); } 4.1(3) #i nclude<stdio.h>main (){int a,b;scan f("%2d%*2s%2d", &a,&b); printf("%d,%d\n",a,b);}4.2 #i nclude<stdio.h>main (){long a,b;float x,y;scan f("%d,%d\n", &a,& b);scan f("%f,%f\n", &x, &y);prin tf("a=%d,b=%d\n",a,b); printf("x=%f,b=%f\n",x,y);}5.1 #i nclude<stdio.h>main (){float a;prin tf("I nn puta:");scan f("%f", &a);if(a>=0){a=a;prin tf("a=%f\n",a);}else{a=-a;prin tf("a=%f\n",a);}}5.2 #i nclude<stdio.h>main (){int a;prin tf("I nputa:");scan f("%d",&a);if(a%2==0){printf("a 是偶数");}else{printf("a 是奇数");}}5.3 #in clude<stdio.h>#in clude<math.h>main (){float a,b,c,s,area;prin tf("I nputa,b,c:");scan f("%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 #i nclude<stdio.h> #in clude<math.h>main (){float a,b,c,x,y;prin tf("I nputa,b,c:");scan f("%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);prin tf("x=%f,y=%f\n",x,y);}else if(b*b-4*a*c==0){x=-b/(2*a);y=-b/(2*a);prin tf("x=%f,y=%f\n",x,y);}else{printf("该方程无实根\n");}}5.5 #in clude<stdio.h>main (){int year,flag;prin tf("I nput a year:");scan f("%d", &year);if(year%4==0&&year%400!=0||year%400==0) { flag=1;}else{flag=0;}if(flag==1)prin tf("%d is a leap year !\n ",year);else{prin tf("%d is not a leap year !\n ",year);}}5.6 #in clude<stdio.h>main (){int year,flag;prin tf("I nput a year:");scan f("%d", &year);flag=year%400==0||year%4==0&&year%100!=0?1:0;if(flag==1 &&flag!=0){prin tf("%d is a leap year !\n ",year);}else{prin tf("%d is not a leap year !\n ",year);}}5.7 #in clude<stdio.h>main (){char ch;prin tf("I nputch:");scan f("%c",&ch);if(ch>='a'&&ch<='z'){ch=getchar();ch=ch-32;prin tf("%c,%d\n",ch,ch);}else if(ch>='A '&&ch<='Z'){ch=getchar();ch=ch+32;prin tf("%c,%d\n",ch,ch);}else{prin tf("%c",ch);5.8 #in clude<stdio.h>main (){char ch;prin tf("I nputch:");scan f("%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 #in clude<stdio.h>main (){int score,grade;prin tf("I nput score:");scan f("%d", &score); grade=score/10;if(score<0||score>100){prin tf("I nput error'n ”);}if(score>=90&&score<=100){prin tf("%d--A\ n",score);}else if(score>=80&&score<90){prin tf("%d--B\n",score);else if(score>=70&&score<80) {prin tf("%d--C\n",score);}else if(score>=60&&score<70) {prin tf("%d--D\n",score);}else if(score>=0&&score<60){prin tf("%d- -E\n",score);}}5.10 #i nclude<stdio.h>main (){int year,m on th;prin tf("I nput year,m on th:");scan f("%d,%d", &year,&mon th); if(mo nth>12||mo nth<=0){prin tf("error mon th\n");}else{switch(year,m on th){case 12:case 10:case 8:case 7:case 5:case 3:case 1:prin tf("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:prin tf("I nput error'n ”);}}}6.1(1) #i nclude<stdio.h>main (){int i,j,k;char space='';for(i=1;i<=4;i++){for(j=1;j<=i;j++){prin tf("%c",space);}for(k=1;k<=6;k++){prin tf("*");}prin tf("\n");}}6.1(2) #in clude<stdio.h>main (){int k=4, n;for(n=0;n<k;n++){if(n %2==0) continue;k--;}prin tf("k=%d\n, n=%d\n",k, n);main (){int k=4, n;for(n=0;n<k;n++){if(n %2==0) break;k--;}prin tf("k=%d, n=%d\n",k ,n);}6.2(1) #in clude<stdio.h>main (){int i,sum=0;for(i=1;i<=101;i++){sum=sum+i;}prin tf("sum=%d\n",sum);}6.2(2) #i nclude<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;}prin tf("sum=%ld\n",sum);}6.2(4) #in clude<stdio.h>#in clude<math.h>main (){int n=1;float term=1.0,sig n=1,sum=0; while(term<=-1e-4||term>=1e-4) {term=1.0/sig n; sum=sum+term; sig n=sig n+n;n++;}prin tf("sum=%f\n",sum);}#in clude<math.h>main (){int n=1,co un t=1;float x;double sum,term;prin tf("I nput x:");scan f("%f", &x);sum=x;term=x;do{term=-term*x*x/(( n+1)* (n+2));sum=sum+term;n=n+2;coun t++;}while(fabs(term)>=1e-5);prin tf("s in( x)=%f,co un t=%d\n",sum,co un t);}6.3 #i nclude<stdio.h>main (){int x=1,fi nd=0;while(!fi nd){if(x%2==1 && x%3==2&& x%5==4&&x%6==5&&x%7==0) {prin tf("x=%d\n",x);fin d=1;x++;}}}/* int x,fi nd=0;for (x=1;!fi nd;x++){if(x%2==1 && x%3==2&& x%5==4&&x%6==5&&x%7==0) {prin tf("x=%d\n",x);fin d=1;}*/6.4 #i nclude<stdio.h>main (){int i,n;long p=1,m=1;prin tf("I nput n:");scan f("%d",&n);for(i=1;i<=n ;i++){ p=i*i; m=i*i*i;prin tf("p=%d,m=%d\n",i,p,i,m);}}6.5 #in clude<stdio.h>main (){float c,f;for(c=-40;c<=110;c=c+10){ f=9/5*c+32;prin tf("f=%f\n",f);}}6.6 #i nclude<stdio.h>#in clude<math.h>main (){int n;double c=0.01875,x;do{x=x*pow(1+c,12)-1000;n++;}while(x>0);prin tf("x=%d\n",x);}6.7 #i nclude<stdio.h>main ()int n=0;float a=100.0,c;prin tf("l nputc:");scan f("%f", &c);do{a=a*(1+c);n++;}while(a<=200); prin tf(" n=%d\n", n);}6.8 #in clude<stdio.h>#in clude<math.h>main (){int n=1,co un t=1;double sum=1,term=1;while(fabs(term)>=1e-5){term=pow(-1,cou nt)*(1.O/( n+2)); sum=sum+term;n=n+2;coun t++;}sum=4*sum;printf( "sum=%f,co un t=%d\n",sum,co un t);}6.9 #in clude<stdio.h>#in clude<math.h>main (){int n=1,co un t=1;double sum=1,term=1;while(fabs(term)>=1e-5){term=term*(1.0/n);sum=sum+term;n++;coun t++;}prin tf("sum=%f,co un t=%d\n",sum,co un t);}6.10 #i nclude<stdio.h> #in clude<math.h> main (){int兀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 #i nclude<stdio.h>main (){int i=0, n;long sum=0,term=1;prin tf("I nput n:");scan f("%d",&n);do{i++;term=term*i;sum=sum+term;}while(sum <n);prin tf("%d\n",i-1);}6.12 #i nclude<stdio.h>main (){int i,n, m,co un t=0,sum=0;prin tf("I nput n:");scan f("%d",&n);for(i=1;i<=n ;i++){prin tf("I nputm:");scan f("%d",&m);if(m>0){sum=sum+m;coun t++;}else{break;}prin tf("sum=%d,co un t=%d\n",sum,co un t);}6.13 #i nclude<stdio.h>main (){int i,n, m,co un t=O,sum=O;prin tf("I nput n:");scan f("%d",&n);for(i=1;i<=n ;i++){prin tf("I nputm:");scan f("%d", &m); if(m>0||m<0){sum=sum+m;coun t++;}else{break;}prin tf("sum=%d,co un t=%d\n",sum,co un t);}}6.14 #in clude<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==5 0&&x+y+z==30) printf("x=%d,y=%d,z=%d\n",x,y,z);}}}6.15 #i nclude<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){prin tf("x=%d,y=%d\n",x,y);}}}6.16 #i nclude<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){prin tf("x=%d,y=%d,z=%d\n",x,y,z);}}}}6.17 #in clude<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==5 0&&z>0){prin tf("x=%d,y=%d,z=%d\n",x,y,z);7.1 #i nclude<stdio.h>int Square(i nt i){return i*i;}int mai n(){int i=0;i=Square(i);for( ;i<3;i++){static int i=1; i+=Square(i); prin tf("%d,",i);}prin tf("%d\n",i);return 0;}7.2 #i nclude<stdio.h>int hour,m inu te,sec ond;void update(){second++;if(sec on d==60){second=0;minu te++;}if(mi nute==60){minu te=0;hour++;} if(hour==24) hour=0;}void display(){prin tf("%d,%d,%d\n" ,hour,mi nute,seco nd);}void delay(){int t;for(t=0;t<100000000;t++);}int mai n()int i;void updaye(),display(),delay(); for(i=0;i<1000000;i++) {update(); display();delay();}return 0;}7.3 #i nclude<stdio.h>int GetMax(i nt a,i nt b);int mai n(){int x,y,ma x;prin tf("I nputx,y:");scan f("%d,%d", &x,& y); max=GetMax(x,y);prin tf("max=%d\n",max); return 0;}int GetMax(i nt m,i nt n){if(m>=n) return m;elsereturn n;}7.4 #in clude<stdio.h>int LCM(i nt n,i nt m);int mai n(){int a,b;prin tf("I nputa,b:");scan f("%d,%d", &a,&b);prin tf("%d\n" ,L CM(a,b)); return 0;}int LCM(i nt n,i nt m)int兀int fin d=0;for(x=1;!fi nd;x++){if(x% n==0&& x%m==O){fin d=1;}}}7.5 #in clude<stdio.h>long Fact(i nt n);int mai n(){int m,a;prin tf("I nputm:");scan f("%d",&m);for(a=1;a<=m;a++){prin tf("%d!=%ld\n",a,Fact(a));}return 0;}long Fact(i nt n){int i;long result=1;for(i=2;i<=n ;i++)result*=i;return result;}7.6 #in clude<stdio.h>long Fact(i nt n);int mai n(){int m;long ret;prin tf("I nputm:");scan f("%d",&m);ret=Fact(m);prin tf("ret=%d\n",ret);}long Fact(i nt n){int i;long result=1,sum=0;for(i=2;i<=n ;i++){result*=i; sum=sum+result;} return sum;}7.7(1) #i nclude<stdio.h>int Gcd(i nt a,i nt b);int mai n(){int m,n;prin tf("I nputm ,n:”);scan f("%d,%d", &m,&n); prin tf("%d\n",Gcd(m, n)); return 0;}int Gcd(i nt a,i nt b){int t,m in,fin d=0;min=a<b?a:b;t=mi n;for(t=mi n;!fi nd;t--){a%t==0;b%t==0;if(a%t==0&&b%t==0) return t;}fin d=1;}7.7(2) #in clude<stdio.h>int Gcd(i nt a,i nt b);int mai n()int m,n;prin tf("I nputm ,n:”);scan f("%d,%d", &m,&n);prin tf("%d\n",Gcd(m, n)); return 0; }int Gcd(i nt a,i nt b){int r,temp; r=a%b;if(r==0)return b;elsedo{temp=b; b=r;a=temp; r=a%b;}while(r!=O); return b;}8.1(1) #i nclude<stdio.h>void Fun c(i nt x){x=2;}int mai n(){int x=10;Fun c(x);prin tf("%d",x); return 0;}8.1(2) #in clude<stdio.h>void Func(int b[]){int j;for(j=0;j<4;j++){b[j]=j;}}int mai n()static int a[]={5,6,7,8},i; Fun c(a);for(i=0;i<4;i++)}return 0;}8.2(1) int PositiveNum(i nt a[],i nt n){int i,co un t=0;for(i=0;i< n;i++){if(a[i]>0)coun t++;}return 0;}8.2(2) void Fib(lo ng f[],lo ng 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) #in clude<stdio.h>int mai n(){int a[10], n, max,mi n, maxPos,mi nPos; for(n=0;n <10; n++) {scan f("%d", &a[n ]);}max=min=a[0];maxPos=min Pos=0;for(n=0;n <10; n++){if(a[ n]>max){max=a[ n];maxPos=n;}else if(a[ n]<mi n)min=a[n];min Pos=n;}prin tf("max=%d,pos=%d\n",max,maxPos);prin tf("mi n=%d,pos=%d\n",mi n, mi nPos); return 0;}8.3 void DivArray(i nt *pArray,i nt n){int i;for(i=0;i< n;i++){pArray[i]/=pAttay[O];}}8.4 #i nclude<stdio.h>#defi ne N 40int FailNum(i nt score[],i nt n);void ReadScore(i nt score[],i nt n);int mai n(){int score[N], n;prin tf("I nput n:");scan f("%d",&n);ReadScore(score ,n);FailNum(score ,n);prin tf("FailNum stude nts are %d\n" ,FailNum(score, n));return 0;}int FailNum(i nt score[],i nt n){int i,co un t=0;for(i=0;i< n;i++){if(score[i]<60)coun t++;}retur n count;}void ReadScore(i nt score[],i nt n)int i;prin tf("I nput score:");for(i=0;i< n;i++)scan f("%d",& score[i]);}}8.5 #in clude<stdio.h>#defi ne N 40int HighAver(i nt score[],i nt n);int ReadScore(i nt score[]);int mai n(){int score[N], n;n=ReadScore(score);prin tf("HighAver stude nts are %d\n",HighAver(score, n));return 0;}int HighAver(i nt score[],i nt n){int i,co un t=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)coun t++;}retur n count;}int ReadScore(i nt score[]){int i=-1;do{i++;prin tf("I nput score:");scan f("%d", &score[i]);}while(score[i]>=0);return i;}8.6 #in clude<stdio.h>#defi ne N 40int ReadScore(i nt score[],l ong nu m[]);int Fin dMax(i nt score[],l ong nu m[],i nt n);int mai n(){int score[N], n, max ,nu m,t;long num [N];t=Fin dMax(score ,n);max=score[t];num=nu m[t];prin tf("max=%d, num=%d\n",max, nu m); return 0;}int ReadScore(i nt score[],l ong nu m[]){int i=-1;do{i++;prin tf("I nput stude nt's ID and score:"); scan f("%ld%d",&nu m[i], &score[i]);}while( nu m[i]>0&&score[i]>=0); return i;}int Fin dMax(i nt score[],l ong nu m[],i nt n){int max,i;max=score[0];for(i=1;i< n; i++){if(score[i]>max) max=score[i];}return i;}8.7 #i nclude<stdio.h>#defi ne N 40int Read(i nt a[]);int Chan geArry(i nt a[],i nt n);int mai n(){int a[N] ,n;prin tf("%d\n",Cha ngeArry(a, n)); return 0;}int Read(i nt a[])int i;for(i=0;i<9;i++){prin tf("I nput a:"); scan f("%d", &a[i]);return i;}int Chan geArry(i nt a[],i nt n){int max,mi n,i,maxpos,mi npos,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];min pos=n; temp=maxpos; maxpos=min pos;min pos=temp;} return a[i];}8.10 #in clude<stdio.h>#defi ne N 5void mai n(){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++){scan f("%d",&a[i][j]);}}for(i = 0; i < N; i++)for(j = 0; j < N; j++){if(i == j II i + j == N - 1) sum=sum+a[i][j];}}printf("对角线元素之和为:%d\n",sum);}8.11 #i nclude<stdio.h>#defi ne N 2#defi ne M 3void mai n(){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++){scan f("%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++){scan f("%d",&b[i][j]); sum2=sum2+a[i][j];}}sum=sum1+sum2;prin tf("sum=%d\n",sum);}8.12 #in clude<stdio.h>#defi ne N 7void Yan gHui(i nt a[N][N],i nt n);void mai n(){int i,j,n ,b[N][N];prin tf("I nput n:");scan f("%d",&n);Yan gHui(b, n);for(i=0;i< n;i++)for(j=0;j<=i;j++){prin tf("%4d",b[i][j]);}prin tf("\n");}}void Ya ngHui(i nt a[N][N],i nt n){int i,j;for(i=0;i< n;i++){ a[i][O]=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 #in clude<stdio.h>#defi ne N 12void mai n(){int i;int f[N]={1,1};prin tf("%d:%d\n",O,f[O]);prin tf("%d:%d\n",1,f[1]);for(i=2;i<N;i++){f[i]=f[i-1]+f[i-2];prin tf("%d:%d\n",i+1,f[i]);}}8.14 #i nclude<stdio.h>#in clude<stdlib.h>#in clude<time.h>#defi ne N 6000void mai n()int a[N],i;int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0;%.2f\n",p1); %.2f\n",p2); %.2f\n",p3); %.2f\n",p4); %.2f\n",p5); %.2f\n",p6);float p1,p2,p3,p4,p5,p6;sran d(time(NULL));for(i=0;i<N;i++){a[i]=ra nd()%6+1;prin tf("%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++;p 仁(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的概率printf("骰子出现2的概率printf("骰子出现3的概率printf("骰子出现4的概率printf("骰子出现5的概率printf("骰子出现6的概率 }9.1(1) void Swap(i nt *x,i nt *y) {int *pTemp;//*pTemp 未进行初始化,*pTemp 指向哪里位置*pTemp=*x;*x=*y;*y=*pTemp;} 9.1(2) void Swap( int *x,i nt *y)int *pTemp; pTemp=x; x=y;y=pTemp;}〃错误,x,y没有指向类型9.2 #i nclude<stdio.h>int mai n(){int a[]={1,2,3,4,5};int *p=a;prin tf("%d,%d,%d,%d,%d,%d,%d\n",*p,*(++p),(*p)++,*p,*p--,--(*p),*p); return 0;}9.3 #in clude<stdio.h>void Swap(i nt *x,i nt *y);int mai n(){int a[6]={1,2,3,4,5,6},b[6]={11,22,33,44,55,66};int i;prin tf("Before Array \n");for(i=0;i<6;i++){printf(" %d\n",a[i]);}for(i=0;i<6;i++){prin tf("%d\n",b[i]);}for(i=0;i<6;i++){Swap(&a[i],&b[i]);}prin tf("After Array \n");for(i=0;i<6;i++){prin tf("%d\n",a[i]);}for(i=0;i<6;i++){prin tf("%d\n",b[i]);}return 0;}void Swap(i nt *x,i nt *y)int temp;temp=*x;*x=*y;*y=temp;}9.4 #in clude<stdio.h>#defi ne N 10void Fin dMaxMi n(int a[],i nt *pMaxa,i nt *pMax num,i nt *pMi na,i nt *pMi nn um);int mai n(){int a[N];int i,maxa,mi na,max num ,mi nnum;prin tf("I nput a:");for(i=0;i<10;i++){scan f("%d", &a[i]);}Fi ndMaxMi n(a,&maxa, &max num,&mi na,&mi nnu m);prin tf("maxa=%d,max num=%d,mi na=%d,mi nnum=%d\n",maxa,max num ,mi na,mi nnu m); return 0;}void Fin dMaxMi n(i nt a[],i nt *pMaxa,i nt *pMax num,i nt *pMi na,i nt *pMi nn um){int i;*pMaxa=a[0];*pMi na=a[0];*pMax num=0;*pMi nn um=0;for(i=0;i<10;i++){if(a[i]>*pMaxa){*pMaxa=a[i];*pMax num=i;}else if(a[i]<*pMi na){*pMi na=a[i];*pM innum=i;}}}10.1 void MYStrcpy(char *dstStr,co nst char *srcStr)while ((*dstStr++=*srcStr++)!='\O')}10.2 #i nclude<stdio.h>main (){char *a="mai n(){char *a=% c% s% c; prin tf(a,34,a,34);}";pri ntf(a,34,a,34);} 10.3(1) un sig ned int MyStrle n(char *p)main (){un sig ned int len;len=0;for(;*pa!='\0';pa++){len++;}return len;}10.3(2) un sig ned int MyStrle n(char s[]){char *p=s;while(*p!='\0'){p++;}return (p-*p);}10.3(3) int MyStrcmp(char *p1,char *p2){for(;*p 仁=*p2;p1++,p2++){if(*p1=='\0') return -1;}return 1;}10.3(4) int MyStrcmp(char s[],char t[]){int i;for(i=0;s[i]==t[i];i++){if(s[i]=='\0') return 0;}return i;}10.3(5) #i nclude<stdio.h>#in clude<stri ng.h>int mai n(){char password[7]="secret";char userI nput[81];prin tf("I nput Password:");sca nf("%s",userl nput);if(strcmp(userl nput,password)==0)prin tf("Coorrect password!Welcome to the system...' n");else if(strcmp(userl nput,password)<0)prin tf("l nvalid password!user in put<password...\n"); elseprin tf("l nvalid password!user in put>password...\n"); return 0;}10.4 #in clude<stdio.h>#defi ne N 24un sig ned int Coun tLetter(char str[]);int mai n(){char a[N];prin tf("l nput a letter:' n");gets(a);prin tf("The len gth of the letter is:%d\n",Cou ntLetter(a)); return 0; }un sig ned int Coun tLetter(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 #in clude<stdio.h>typedef struct data{int year;int mon th;int day;}DATA;typedef struct work{char a[14];char b[1O];char c[6];}WORK;typedef struct stude nt{char stude ntName[10];char stude ntSex;DATA birthday;WORK professio n;}STUDENT;int mai n(){STUDENT stu={"王刚”,'M',{1991,5,19},{"信息工程系","学习人员",”学生"}};prin tf("stu:%2s%3c%6d/%02d/%02d%4s,%4s,%4s\n",stu.stude ntName,stu.stude ntSex,stu.birthday.year,stu.birthday. mon th,stu.birthday.day,stu.professio n.a,stu.professio n.b,stu.professi on .c);return 0;}12.4 #in clude<stdio.h>#in clude<stri ng.h>#defi ne N 10typedef struct people{char peopleName[10];}PEOPLE;int mai n(){int sum1=0,sum2=0,sum3=0,sum4=0;int i;char name[N][N];PEOPLE stu[5]={"zha ng","li","wa ng"};prin tf("E nter their n ame:\n");for(i=0;i<10;i++){gets (n ame[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++;}prin tf("stu:%4s%2d\n",stu[i].peopleName,sum[j]);prin tf("stu:%4s%2d\n",stu[1].peopleName,sum2);prin tf("stu:%4s%2d\n",stu[2].peopleName,sum3);prin tf("%2d\n",sum4);return 0;}。

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

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

2.2#iiiclude<stdio.h>main(){float x=2・5,y=2・5,z=2・5;pnntf(M x=%far\x); pnntf(M y=%fur\y);pmitf(”z=%f\n”,z);}3.1(1) #iiiclude<stdio.h> main(){int a=12,b=3;float x=18・5,y=4・6;printf(,,%d,di,\(float)(a*b)/2); pmitf(・%d\n役(int)x%(mt)y);}3.2#iiiclude<stdio.h>main(){int x,b0,bl,b2,s; pnntf(M Iiiputx:H);scanfC%dt&x);b2=x/100; bl=(x-b2*100)/10;b0=x%10; s=b0*100+bl*10+b2;}3.3# iiiclude<stdio.h>#mclude<math.h>main(){float rate=0.0225;float n,capital,deposit;printf(M Iiiput n,capital:H);scanf("%f,%f;&n,&capital);deposit=capital*pow(l+iate,n);printf(M deposit=%fji'\deposit);}3.4#iiiclude<stdio.h>#mclude<math.h>main()float 久b,c;double x,y;printf(M Iiiputa,b,c:H);scanf(H%f,%f,%f\&a.&b、&c);x=(-b+sqit(b*b-4*a*c))/(2 *a);y=(-b-sqrt(b*b-4*a*c))/(2*a); pnntfC,x=%fy=%fur\x,y);}4.1(1) #iiiclude<stdio.h>main(){char cl=,a\c2=,b\c3=t c,;printf(,l a%cb%cc%c\n,\c 1 ,c2,c3);}4.1(2) #iiiclude<stdio.h>main(){int a=12,b=15;pmHfTa=%d%%,b=%d%%S”,a,b);}4.1(3) #iiiclude<stdio.h>main(){int a、b;scanfC%2d%*2s%2d”,&a,&b);piiDtfp%d.%d\ir;a,b);}4.2#iiiclude<stdio.h>main(){long a,b;float x、y;scanfC%d,%d\n 役&a.&b);scanf(H%f,%fur\&x.&y);piintf(M a=%d.b=%d,,ii,\a.b); pnntfC,x=%fb=%far\x,v);}5.1 #iiiclude<stdio.h>main(){float a;scanf(H%f\&a);if(a>=0)a=a; pnntf「a=%f\n=);}else{a=-a; pnntf「a=%f\n=);}}5 ・ 2 #iiiclude<stdio.h>main(){int a;pnntf(H Iiiputa:H);scanff%cT;&a);if(a%2==0){prmtf("a 是偶数”);}else{piuitf("a 是奇数”);}}5.3#iiiclude<stdio.h>#mclude<math.h>main(){float a,b,c,s,area; pnntH M Iiiputa,b,c:H);scanf(H%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));priiitf(n aiea=%f.n,\area);}else{pnntf(”不是三角形”);}}5.4#iiiclude<stdio.h>#iiiclude<math.li>mam()float a、b,c,x,y;pmitfClnputabcJ);if(a=0){ pnntf(”该方程不是一元二次方程®”);}if(b*b-4*a*c>0){ x=(-b+sqrt(b *b-4*a *c))/(2*a);v=(-b-sqrt(b*b-4*a*c))/(2*a); pnntf「x=%f,y=%fm役x,y);}else if(b*b-4*a*c=0){x=-b/(2*a);v=-b/(2*a); pnntf「x=%f,y=%fm 役x,y);}else{ pnntf(”该方程无实根\n”);}}5.5#iiiclude<stdio.h>main(){int yeai;flag;printf(M Iiiput a vear:H);scanf^^d^&yeai);if(y 亡ai%4==0&&yeai-%400! =01 |year%400==0) { flag=l;}else{flag=0;}if(flag==l)piiiitf(n%d is a leap year !\n”,yeaQ;elsepiiiitf(,,%d is not a leap year l^^year);}}5.6#iiiclude<stdio.h>main(){int yeai;flag;printf(M Iiiput a vear:H);scanfl^d^&yeai);flag=veai-%400=0||year%4==0&&y 亡ai%100!=0H:0;if(flag==l && flag! =0){priiitf(n%d is a leap year !\n,\vear);}else{is not a leap year l^^year);}}5.7#iiiclude<stdio.h>main(){char ch;pnntf(M Iiiputch:H);scanf(”%c-&ch);if(ch>=,a,&&ch<=N){ch=getchai();ch=ch・32;printf(n%c,%d\n,\ch,ch);}else if(ch>=,A,&&cli<='Z f){ch=getchai();ch=ch+32;printf(n%c,%d\n,\ch,ch);}elsepnntff%L,ch);5 ・ 8 #iiiclude<stdio.h> main()char ch; pnntf(M Iiiputch:H); scanf(”%ct&ch);if(ch>=48&&ch<=57){pruitf("ch是数字字符\n");}else if(ch>=65&&ch<=90){pimtfC'ch是大写字母\n");}else if(ch>=97&&ch<=122){ pnntf(”ch是小写字母\n");}else if(ch==32){printf(” ch 是空格\n”);}else{ pimtfC'ch是其他字符\n”);}}5.9 #iiiclude<stdio.h>main(){int score,grade; printf(M Iiiput score:n); scanf(H%d,\&scoie);grade=score/10; if(score<0|| score> 100){priiitf(n Input enor\n H);}if(score>=90&&score<= 100){prmtf(H%d-A\ii,\score);}else if(score>=80&&score<90)(n \H<ormuud&use。

零基础入门学习C语言课件第十一章 文件操作1

零基础入门学习C语言课件第十一章 文件操作1
文件通常是驻留在外部介质(如磁盘等)上的, 在使用时才调入内存中来。从不同的角度可对 文件作不同的分类。从用户的角度看,文件可 分为普通文件和设备文件两种。
小小图例
操作系统是以文件为单位对数据进行管理的。
输出文件缓冲区
程序 数据区
文件
输入文件缓冲区
文件的分类
ቤተ መጻሕፍቲ ባይዱ用户观点:
特殊文件(标准输入输出文件或标准设备文 件)
(读写)为读/写建立一个新的二进制文件 (读写)为读/写打开一个二进制文件
对于文件使用方式有以下几点说明
凡用“r”打开一个文件时,该文件必须已经存 在,且只能从该文件读出。
用“w”打开的文件只能向该文件写入。若打 开的文件不存在,则以指定的文件名建立该文 件,若打开的文件已经存在,则将该文件删去, 重建一个新文件。
(只读)为输入打开一个文本文件 (只写)为输出打开一个文本文件 (追加)向文本文件尾增加数据 (只读)为输入打开一个二进制文件 (只写)为输出打开一个二进制文件 (追加)向二进制文件尾增加数据 (读写)为读/写打开一个文本文件 (读写)为读/写建立一个新的文本文件 (读写)为读/写打开一个文本文件 (读写)为读/写打开一个二进制文件
通过该结构体变量中的文件信息能够访问该文 件。 如果有n个文件,一般应设n个指针变量,使 它们分别指向n个文件,以实现对文件的访问。
文件的打开与关闭
FILE类型的数组: FILE f[5]; 定义了一个结构体数组f,
它有5个元素,可以用来存放5个文件的 信息。
一头雾水?不明白?接着看具体的使用 就明白啦~
课外知识
在UNIX系统下,用缓冲文件系统来处理文 本文件,用非缓冲文件系统来处理二进 制文件。
ANSI C 标准只采用缓冲文件系统来处理 文本文件和二进制文件。

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

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

2.2#include<stdio.h>main(){float x=2・5,y=2・5,z=2・5;printf(H x=%f\n'\x); printf(,t y=%f\n,,,y);printf(H z=%f\n'\z);}3.1(1) #include<stdio.h>niain(){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>niainO{int x.bO・bl.b2,s; printf(H Inputx:H);scanf(n%d H.&x); b2=x/100;bl=(x-b2* 100)/10; b0=x%10;s=b0*100+bl*10+b2;printf(,,s=%d\n,\s);)3.3#include<stdio.h>#include<math.h>inain(){float rate=0.0225;float n.capital.deposit; printf(n Inputn,capital:”);scanf(n%f,%f\&n,&capital);deposit=capital*pow( 1 +rate,n);printf(M deposit=%f\n,\deposit);}3.4#include<stdio.h> #include<math.h> main(){float a,b,c;double x,y;primf(”Inpu(a.b、c:”);scanf(M%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(H x=%ty=%f\n M,x,y);}4.1(1) #include<stdio.h>niain(){char c I='if,c2=b,c3='c:printf(°a%cb%cc%c\n,\c 1 ,c2,c3);}4.1(2) #include<stdio.h>niain()int a=12,b=15;printf(H a=%d%%,b=%d%%\n\a,b); )4.1(3) #include<stdio.h> main(){int a,b;scanf(n%2d%*2s%2d M,&a.&b); printf(u%d.%d\n,\a,b); }4.2 #include<stdio.h>niain(){long a.b;float x,y; scanf(n%d,%d\n*\&a,&b);scanf(,,%f,%f\n,\&x.&y); printf(u a=%d,b=%d\n,\a,b):printf(H x=%tb=%f\n,\x,y);}5.1#include<stdio.h>niain(){float a;printf(u Innputa:H); scanf(M%f\&a);if(a>=0){a=a:printf(H a=%f\n H,a);}else{a=-a;printf(,,a=%f\n,\a);})5.2#include<stdio.h>main(){int a;printf(H Inputa:M);scanf(M%d M.&a);if(a%2==0){printf(°a 是偶数”);}else{ printf(u a 是奇数”);}}53 #include<stdio.h>#include<math.h>inain(){float a,b,c,s,area;primf(”Inpu(abc:”); scanf(H%f,%L%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(H不是三角形”);}}5.4#include<stdio.h>#include<math.h>niain(){float a.b、c,x,y;printf(,,Inputa.bx:H);if(a==O){primf(”该方程不是一元二次方程\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 H,x,y);}else if(b*b-4*a*c==0){x=-b/(2*a);y=-b/(2*a): printf(,,x=%fy=%f\n H,x,y);}else{ printfC*该方程无实根\n”);})5.5#include<stdio.h>main(){int yearflag;printf(n Input a year:0); scanf(,,%d M.&year);if(year%4==0&&vear%400!=0llyear%400==0) {flag=l;}else{flag=O;} if(flag=l){- printf(H%d is a leap year !\n H,year);}else{ printf(H%d is not a leap year !\n M,year);}}5.6#include<stdio.h>main()int yeanflag; printf(M Input a year:0); scanf(,,%d H,&year);flag=year%400==0llyear%4==0&&year%100!=0? 1:0;if(flag=l&&flag!=O){〜 ~printf(u%d is a leap year !\n f\vear);}else{ printf(u%d is not a leap year !\n*\year);}}5.7#include<stdio.h>inain() { char ch: printf(°I nputch:"); scanf(H%c*\&ch);if(ch>=,a,&&ch<=,z,) { ch=getchar(); ch=ch-32;printf(n%c.%d\n'\ch,ch);}else if(ch>=*A'&&chv=Z){ch=getchar(); ch=ch+32;printf(H%c.%d\n,\ch,ch);}else{ primiT%c”,ch);}}5.8#include<stdio.h>main() {char ch;printf(u I nputch:,r); scanf(”%c”,&ch);if(ch>=48&&ch<=57){printf(Hch是数字字符\nj;}else if(ch>=65&&ch<=90){printf(Hch是大写字母\nj;}else if(ch>=97&&ch<=122){printf(Hch是小写字母\nj;}else if(ch=32)printf(°ch 是空格\nj;}else printf(°ch是瓦他字符\n J;}5.9#include<stdio.h>main(){int score.gradc; printf(u Input score:H);scanf(H%d H,&score);grade=score/10; if(score<Ollscore> 100){printfC'I nput error\n H);}if(score>=90&&score<=100){ printf(u%d-A\n,\score);}else if(score>=80&&score<90){printf(u%d-B\n,\score);}else if(score>=70&&score<80){printf(,,%d-C\n M,score);}else if(score>=60&&score<70){printf(H%d-D\n H,score);}else if(score>=0&&score<60){ printf(u%d- -E\n M,score);}}5.10#include<stdio.h>inain(){int yeanmonth: printf(°Input year,month:”);scanf(H%d,%d H.&yean &m onth); if(month> 12llmonth<=0) {printf(H error month\n M);}else{switch(year,month){case 12:case 10:case &case 7:case 5:case 3:case 1:printf(,r31 天5"); break;case 11:case 9:case 6:case 4:printf(n30 天\n»);break:case 2:if(year%4==0&&year!=0llyear%400==0) {printf(H29 天\nj;}else{ printf(H28 天\nj;}break;default:printf(*'I 叩ut error\n H);}}}6.1(1) #include<stdio.h>inain(){int ij,k;char space=* :for(i=l;i<=4;i++){ for(j=l;j<=i;j++) Iprimf("%c”,space);} for(k=l;k<=6;k++) {printfC*");} printf(°\n M);})6.1 (2) #include<stdio.h>main(){int k=4,n;for(n=0;n<k:n++){if(n%2==0) continue: k—;} printf(u k=%d\nji=%d\n,\k,n);}6.1(3) #include<stdio.h>niain(){int k=4,n;for(n=0;n<k:n++){if(n%2==0) break; k—;}printf(u k=%d.n=%d\n,\k.n);}6.2(1) #include<stdio.h>niain()int i,sum=O;for(i=l;i<=101;i++){ sum=sum+i;}printf(,,sum=%d\n,\sum);}6.2(2) #include<stdio.h>main(){long i;long ternksum=O;for(i= I ;i<= 101 ;i=i+2){ term=i*(i+l)*(i+2); sum=sum+term:} printf(,,sum=%ld\n*\sum);}6.2(4) #include<stdio.h>#include<math.h>niain(){int n=l;float term= 1.0.sign= 1 ,sum=O; while(term<=-1 e-4llterm>= 1 c-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>inain(){int n=Lcount=l;float x;double suniterni: printfCI nput x:N); scanf(H%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)>= 1 e-5); printf(H sin(x>%f,count=%d\n",sum,count); } 6.3#include<stdio.h>niain(){int x=l,find=0;while(!find)if(x%2==l&&x%3==2&&x%5==4&&x%6==5&&x%7==0)printf(u x=%d\n M,x);find=l;x++;}}}/* int x,find=O:for (x=l;!find;x++){if(x%2==l&&x%3==2&&x%5==4&&x%6==5&&x%7==0) {printf(u x=%d\n M,x);find=l;}}}*/6.4#include<stdio.h>niainO{int i.n;long p=Lm=l;printf(u I nput n:N);scanf(M%d M.&n);for(i=l;i<=n:i++){p=i*i;printf(,,p=%d.m=%d\n,\i.p,i.m);}}6.5#includc<stdio.h>main(){float c,f;for(c=-40;c<=l 10;c=c+10){f=9/5*c+32;printf(,,f=%f\n,\f);}}6.6#include<stdio.h>#include<math.h>niainO{int n;double c=0.01875.x;do{x=x*pow(l+cj 2)-1000;n++;}whiIe(x>0);printf(,,x=%d\n,\x);}6.7#include<stdio.h>niainO{int n=0;float a=100.0,c;printf(H I nputc:,r);scanf(H%f\&c);doa=a*(l+c);n++; }while(a<=200);printf(H n=%d\n,\n);}6.8#include<stdio.h>#include<math.h>main(){int n=l,count=l;double sum= 1 ,term= 1;。

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

《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的阶乘。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
a[0][0] a[0][1]
第i行第j列的地址&a[i][j]
a+1 &a[1][0] a[1]
a[0][2]
a[1][0]
a[1][1]
a
a[1]
&a[1][2] a[1]+2
a[1][2]
13/42
2015-6-19
11.2 指针和二维数组间的关系
*(a + i) 即 a[i] 代表第i行第0列的地址,列地址
a[0][2] a[1][0] a[1][1]
p
2015-6-19
a[1]针和二维数组间的关系

二维数组的列指针
//用列地址初始化
short a[2][3]; *a或 p a[0]
int *p; p = *a;

逐个查找——相对偏移量
a[0][0]
a[0][1] a[0][2] a[1][0] a[1][1]
for (i=0; i<4; i++) printf("%d ", a[i]);
p
0x0037b004
a[0] a[1] a[2] a[3]
0x0037b008
0x0037b00B
4
…...
for (p=a; p<(a+4); p++) scanf("%d", p);
a+4
2015-6-19
for (p=a; p<(a+4); p++) printf("%d ", *p);
char ... name[N][MAX_LEN];
N for (i=0; i<n-1; i++) { for (j = i+1; j<n; j++) { if(strcmp(str[j],str[i])<0) { strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); } } }
19/42
11.2 指针和二维数组间的关系
【例11.3】输入一个3行4列的二维数组,然后输出 这个二维数组的元素值
形参声明为列数已知的 二维数组
2015-6-19
20/42
11.2 指针和二维数组间的关系
【例11.3】输入一个3行4列的二维数组,然后输出 这个二维数组的元素值
形参声明为指向列数已知 的二维数组的行指针
…...
数组元素的等价引用形式 a[i] *(a+i) pa[i] *(pa+i)
2015-6-19
4/42
11.1 指针和一维数组间的关系
int a
0x0037b000
a[4]={1,2,3,4}; 1 *p 2 *p 3
….
for (i=0; i<4; i++) scanf("%d", &a[i]);
a 代表二维数组的首地址,第0行的地址,行地址 a + i 代表第i行的地址 但并非增加i个字节!
short a[2][3];
a[0] a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
a
a[0][0] a[0][1] a[0][2]
a+1
a[1][0]
a[1][1] a[1][2]

GUI界面之前,计算机的操作界面都是字符式的命 令行界面(DOS、UNIX、Linux) 通过命令行参数(Command Line Arguments),使 用户可以根据需要来决定程序干什么、怎么干 main(int argc, char* argv[]) – 当你把main函数写成这样时 – argc的值为:参数的数目+1 – argv[0]为指向命令名的字符指针 – argv[x](x>1)为指向每个参数的字符指针
2015-6-19
N
索引排序
交换指针数组中的字符串指针
28/42
【例11.4】国名字符串排序 ——指针数组
在使用指针数组之前 必须对数组元素进行初始化
2015-6-19
29/42
【例11.4】国名字符串排序 ——指针数组
能这样输入字符串吗?Why?
2015-6-19
30/42
11.3.2指针数组用于表示命令行参数
a *(a+i) + j 即 a[i] + j代表 &a[0][0] a[0] short a[2][3];
a[0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
a[0][0] a[0][1]
第i行第j列的地址&a[i][j] *( *(a+i) + j ) 即 a[i][j] a[0][0] 代表第i行第 j列的内容 a
a[1]
a+1 &a[1][0] a[1]
a[0][2]
a[1][0]
a[1][1]
&a[1][2] a[1]+2
a[1][2]
14/42
2015-6-19
11.2 指针和二维数组间的关系

二维数组的行指针 p
short a[2][3]; a
a[0][0]
a[0][1]
int (*p)[3]; p = a; //用行地址初始化
2015-6-19
21/42
11.2 指针和二维数组间的关系
【例11.3】输入一个3行4列的二维数组,然后输出 这个二维数组的元素值
形参声明为指向二维数组 的列指针
2015-6-19
22/42
11.2 指针和二维数组间的关系
【例11.3】输入一个3行4列的二维数组,然后输出 这个二维数组的元素值
问数组元素
2015-6-19
8/42
11.1 指针和一维数组间的关系
【例11.2】演示数组和指针变量作函数参数
被调函数的形参声明为 指针类型,用指针法访
问数组元素
2015-6-19
9/42
11.1 指针和一维数组间的关系
【例11.2】演示数组和指针变量作函数参数
在主函数中这样做没有 多大的实际意义
12/42
a
a[1]
2015-6-19
11.2 指针和二维数组间的关系
*(a + i) 即 a[i] 代表第i行第0列的地址,列地址
a *(a+i) + j 即 a[i] + j代表 &a[0][0] a[0] short a[2][3];
a[0] a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
for (i=0; i<m; i++) for (j=0; j<n; j++) printf("%d",*(p+i*n+j)); p[i*n+j]
a[1][2]
2015-6-19 18/42
11.2 指针和二维数组间的关系
【例11.3】输入一个3行4列的 二维数组,然后输出这个 二维数组的元素值
2015-6-19
24/42


2015-6-19
指针、数组以及其他的类型混合

基本数据类型 – int、long、char、short、float、double…… 数组是一种数据类型 – 是从其他类型派生的类型


每个元素都有一个类型

指针是一种数据类型 – 是从其他类型派生的类型

XX类型的指针

任何类型都可以作指针或者数组的基类型
a+1
a[0][2] a[1][0] a[1][1]
a[1][2]
2015-6-19 16/42
11.2 指针和二维数组间的关系

二维数组的行指针
short a[2][3]; a
a[0][0]
a[0][1]
int (*p)[3]; p = a; //用行地址初始化

逐行查找-〉逐列查找
a+1
for (i=0; i<m; i++) for (j=0; j<n; j++) printf("%d",*(*(p+i)+j));
5/42
11.1 指针和一维数组间的关系
【例11.1】演示数组元素的引用方法
2015-6-19
6/42
11.1 指针和一维数组间的关系
【例11.1】演示数组元素的引用方法
2015-6-19
7/42
11.1 指针和一维数组间的关系
【例11.2】演示数组和指针变量作函数参数
被调函数的形参声明为 数组类型,用下标法访
2015-6-19
3/42
11.1 指针和一维数组间的关系
int a
0x0037b000
a[4]={1,2,3,4}; 1 pa[0] 2 *(a+1)
….
int *pa=a; int *pa=&a[0];
pa
0x0037b004
a[0] a[1] a[2] a[3]
a+1
0x0037b008
3 pa+2 *(pa+2) 0x0037b00B 4 *pa
2015-6-19
23/42
指针和数组作函数参数

通过指针或数组参数,使调用者获得修改后的 数据
相关文档
最新文档