1、语言程序设计苏小红课后习题答案修订高等教育出版社2.2#includemain()floatx=2.5,y=2.5,z=2.5;printf(x=%fn,x);printf(y=%fn,y);printf(z=%fn,z);3.1(1)#includemain()inta=12,b=3;floatx=18.5,y=4.6;printf(%fn,(float)(a*b)/2);printf(%dn,(int)x%(int)y);3.1(2)#includemain() intx=32,y=81,p,q; p=x+; q=-y; printf(%d%dn,p,q); printf(%d%dn,x,
2、y);3.2#includemain()intx,b0,b1,b2,s;printf(Inputx:);scanf(%d,&x);b2=x/100;b1=(x-b2*100)/10;/或(x%100)/10;或x/10%10;b0=x%10;s=b0*100+b1*10+b2;printf(s=%dn,s);3.3#include#includemain()floatrate=0.0225;floatn,capital,deposit;printf(Inputn,capital:);scanf(%f,%f,&n,&capital);deposit=capital*pow(1+rate,n);p
3、rintf(deposit=%fn,deposit);3.4#include#includemain()floata,b,c;doublex,y;printf(Inputa,b,c:);scanf(%f%f%f,&a,&b,&c);x=(-b+sqrt(b*b-4*a*c)/(2*a);y=(-b-sqrt(b*b-4*a*c)/(2*a);printf(x=%f,y=%fn,x,y);习题44.1(1)#includemain()charc1=a,c2=b,c3=c;printf(a%cb%cc%cn,c1,c2,c3);4.1(2)#includemain()inta=12,b=15;pr
4、intf(a=%d%,b=%d%n,a,b);4.1(3)#includemain()inta,b;scanf(%2d%*2s%2d,&a,&b);printf(%d,%dn,a,b);4.2#includemain()longa,b;floatx,y;scanf(%d,%dn,&a,&b);scanf(%f,%fn,&x,&y);printf(a=%d,b=%dn,a,b);printf(x=%f,b=%fn,x,y);5.1#includemain()floata;printf(Inputa:);scanf(%f,&a);if(a=0)a=a;printf(a=%fn,a);elsea=-
5、a;printf(a=%fn,a);5.2#includemain()inta;printf(Inputa:);scanf(%d,&a);if(a%2=0)printf(a是偶数);elseprintf(a是奇数);5.3#include#includemain()floata,b,c,s,area;printf(Inputa,b,c:);scanf(%f%f%f,&a,&b,&c);if(a+bc&a+cb&b+ca)s=(a+b+c)/2;area=(float)sqrt(s*(s-a)*(s-b)*(s-c);printf(area=%fn,area);elseprintf(不是三角形)
6、;5.4#include#includemain()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*c0)x=(-b+sqrt(b*b-4*a*c)/(2*a);y=(-b-sqrt(b*b-4*a*c)/(2*a);printf(x=%f,y=%fn,x,y);elseif(b*b-4*a*c=0)x=-b/(2*a);y=-b/(2*a);printf(x=%f,y=%fn,x,y);elseprintf(该方程无实根n);5.5#inclu
7、demain()intyear,flag;printf(Inputayear:);scanf(%d,&year);if(year%4=0&year%400!=0|year%400=0)flag=1;elseflag=0;if(flag=1)printf(%disaleapyear!n,year);elseprintf(%disnotaleapyear!n,year);5.6#includemain()intyear,flag;printf(Inputayear:);scanf(%d,&year);flag=year%400=0|year%4=0&year%100!=0?1:0;if(flag=
8、1&flag!=0)printf(%disaleapyear!n,year);elseprintf(%disnotaleapyear!n,year);5.7#includemain()charch;printf(Inputch:);scanf(%c,&ch);if(ch=a&ch=A&ch=Z)ch=getchar();ch=ch+32;printf(%c,%dn,ch,ch);elseprintf(%c,ch);5.8#includemain()charch;printf(Inputch:);scanf(%c,&ch);if(ch=48&ch=65&ch=97&ch=122)printf(c
9、h是小写字母n);elseif(ch=32)printf(ch是空格n);elseprintf(ch是其他字符n);5.9#includemain()intscore,grade;printf(Inputscore:);scanf(%d,&score);grade=score/10;if(score100)printf(Inputerrorn);if(score=90&score=80&score=70&score=60&score=0&score60)printf(%d-En,score);5.10#includemain()intyear,month;printf(Inputyear,mo
10、nth:);scanf(%d,%d,&year,&month);if(month12|month=0)printf(errormonthn);elseswitch(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);elseprintf(28天n);break;default:printf(Inputerrorn)
11、;6.1(1)#includemain()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)#includemain()intk=4,n;for(n=0;nk;n+)if(n%2=0)continue;k-;printf(k=%dn,n=%dn,k,n);6.1(3)#includemain()intk=4,n;for(n=0;nk;n+)if(n%2=0)break;k-;printf(k=%d,n=%dn,k,n);6.2(1
12、)#includemain()inti,sum=0;for(i=1;i=101;i+)sum=sum+i;printf(sum=%dn,sum);6.2(2)#includemain()longi;longterm,sum=0;for(i=1;i=101;i=i+2)term=i*(i+1)*(i+2);sum=sum+term;printf(sum=%ldn,sum);6.2(4)#include#includemain()intn=1;floatterm=1.0,sign=1,sum=0;while(term=1e-4)term=1.0/sign;sum=sum+term;sign=sig
13、n+n;n+;printf(sum=%fn,sum);6.2(5)#include#includemain()intn=1,count=1;floatx;doublesum,term;printf(Inputx:);scanf(%f,&x);sum=x;term=x;doterm=-term*x*x/(n+1)*(n+2);sum=sum+term;n=n+2;count+;while(fabs(term)=1e-5);printf(sin(x)=%f,count=%dn,sum,count);6.3#includemain()intx=1,find=0;while(!find)if(x%2=
14、1&x%3=2&x%5=4&x%6=5&x%7=0)printf(x=%dn,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=%dn,x);find=1;*/6.4#includemain()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=%dn,i,p,i,m);6.5#includemain()floatc,f;for(c=-40;c=
15、110;c=c+10)f=9/5*c+32;printf(f=%fn,f);6.6#include#includemain()intn;doublec=0.01875,x;dox=x*pow(1+c,12)-1000;n+;while(x0);printf(x=%dn,x);6.7#includemain()intn=0;floata=100.0,c;printf(Inputc:);scanf(%f,&c);doa=a*(1+c);n+;while(a=200);printf(n=%dn,n);6.8#include#includemain()intn=1,count=1;doublesum=
16、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=%dn,sum,count);6.9#include#includemain()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=%dn,sum,count);6.10#includ
17、e#includemain()intx;for(x=100;x=999;x+)if(x=pow(x/100,3)+pow(x/10%10,3)+pow(x%10,3)printf(x=%dn,x);6.11#includemain()inti=0,n;longsum=0,term=1;printf(Inputn:);scanf(%d,&n);doi+;term=term*i;sum=sum+term;while(sumn);printf(%dn,i-1);6.12#includemain()inti,n,m,count=0,sum=0;printf(Inputn:);scanf(%d,&n);
18、for(i=1;i0)sum=sum+m;count+;elsebreak;printf(sum=%d,count=%dn,sum,count);6.13#includemain()inti,n,m,count=0,sum=0;printf(Inputn:);scanf(%d,&n);for(i=1;i0|m0)sum=sum+m;count+;elsebreak;printf(sum=%d,count=%dn,sum,count);6.14#includemain()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;
19、if(3*x+2*y+z=50&x+y+z=30)printf(x=%d,y=%d,z=%dn,x,y,z);6.15#includemain()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=%dn,x,y);6.16#includemain()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=%dn
20、,x,y,z);6.17#includemain()intx,y,z;for(x=1;x=9;x+)for(y=1;y0)printf(x=%d,y=%d,z=%dn,x,y,z);7.1#includeintSquare(inti)returni*i;intmain()inti=0;i=Square(i);for(;i3;i+)staticinti=1;i+=Square(i);printf(%d,i);printf(%dn,i);return0;7.2#includeinthour,minute,second;voidupdate()second+;if(second=60)second=
21、0;minute+;if(minute=60)minute=0;hour+;if(hour=24)hour=0;voiddisplay()printf(%d,%d,%dn,hour,minute,second);voiddelay()intt;intmain()inti;voidupdaye(),display(),delay();for(i=0;i1000000;i+)update();display();delay();return0;7.3#includeintGetMax(inta,intb);intmain()intx,y,max;printf(Inputx,y:);scanf(%d
22、,%d,&x,&y);max=GetMax(x,y);printf(max=%dn,max);return0;intGetMax(intm,intn)if(m=n)returnm;elsereturnn;7.4#includeintLCM(intn,intm);intmain()inta,b;printf(Inputa,b:);scanf(%d,%d,&a,&b);printf(%dn,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#includelongFact(intn);intmain()intm,a;printf(Inputm:);scanf(%d,&m);for(a=1;a=m;a+)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1