《[工学]C语言课程的一些答案.doc》由会员分享,可在线阅读,更多相关《[工学]C语言课程的一些答案.doc(40页珍藏版)》请在三一办公上搜索。
1、1-1 #include#includevoid main()int a=3,b=4,c=5;float s;float l;l=(a+b+c)/2.0;s=sqrt(l*(l-a)*(l-b)*(l-c);printf(s=%fn,s); 1-2 #includevoid main()int h,f;int x;int y;scanf(%d%d,&h,&f);x=(f-2*h)/2;y=(4*h-f)/2;printf(x=%d,y=%dn,x,y);1-3 #includevoid main()int r,h;float l,s1,s2,v1,v2;scanf(%d%d,&r,&h);l=
2、3.14*2*r;s1=3.14*r*r;s2=4*3.14*r*r;v1=3.14*r*r*h;v2=4*3.14*r*r*r/3;printf(l=%f,s1=%f,s2=%f,v1=%f,v2=%fn,l,s1,s2,v1,v2);1-4 #includevoid main()int F;float c;scanf(%d,&F);c=5.0/9*(F-32);printf(c=%f,c);1-5 #include#includevoid main()float r=0.008;float n;n=log10(2)/log10(1+r);printf(n=%fn,n);1-6 #inclu
3、de#includevoid main()int x1,y1,x2,y2;float d;scanf(%d%d%d%d,&x1,&y1,&x2,&y2);d=sqrt(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);printf(d=%fn,d);1-7 #includevoid main()printf( *n);printf( *n);printf( *n);printf( *n);printf(*n);2-1#includevoid main()int a,b,t;scanf(%d%d,&a,&b);if(ab)t=a;a=b;b=t;printf(a=%d,b=%dn,a
4、,b);2-2 #includevoid main()int a,b,c,t;scanf(%d%d%d,&a,&b,&c);if(ab)t=a;a=b;b=t;if(ac)t=a;a=c;c=t;if(bc)t=b;b=c;c=t;printf(a=%d,b=%d,c=%dn,a,b,c);2-3 #includevoid main()int a,b,t;scanf(%d%d,&a,&b);if(ab)printf(max=%dn,a);elseprintf(max=%dn,b);2-4 #includevoid main()int x,y;scanf(%d,&x);if(x=1 & x=10
5、)y=x*x+2*x+2;printf(y=%dn,y);2-5 #includevoid main()int mark;char grade;scanf(%d,&mark);if(mark=90)grade=A;if(mark=80 & mark=70 & mark=60 & mark70)grade=D;if(mark60)grade=E;printf(grade=%cn,grade);2-6 #include#includevoid main()int a,b,c,t;float x1,x2;scanf(%d%d%d,&a,&b,&c);t=b*b-4*a*c;x1=(-b+sqrt(t
6、)/(2*a);x2=(-b-sqrt(t)/(2*a);printf(x1=%f,x2=%fn,x1,x2);2-7 #includevoid main()int x,y;scanf(%d%d,&x,&y);if(x0 & y0)printf(firstn);if(x0)printf(secondn);if(x0 & y0 & y0)printf(fourthn);if(x=0)printf(Yn);if(y=0)printf(Xn);2-8 #includevoid main()int a;scanf(%d,&a);if(a%6=9 & a%9=0 & a%14=0)printf(yesn
7、);elseprintf(non);2-9 #includevoid main()int year;scanf(%d,&year);if(year%4=0 & year%100!=0) | (year%400=0)printf(yesn);elseprintf(non);2-10 #includevoid main()int a,b,c,d,t;scanf(%d%d%d%d,&a,&b,&c,&d);if(ab)t=a;a=b;b=t;if(ac)t=a;a=c;c=t;if(ad)t=a;a=d;d=t;if(bc)t=b;b=c;c=t;if(bd)t=b;b=d;d=t;if(cd)t=
8、c;c=d;d=t;printf(a=%d,b=%d,c=%d,d=%dn,a,b,c,d);2-11 #includevoid main()int m,a,b,c;scanf(%d,&m);a=m/100;b=(m%100)/10;c=m%10;if(a*a*a+b*b*b+c*c*c=m)printf(yesn);elseprintf(non);2-12 #includevoid main()int x,y;scanf(%d,&x);if(x=0 & x=10 & x=100)y=x*x;printf(y=%dn,y);2-13 #include#includevoid main()int
9、 a,b;float y;scanf(%d%d,&a,&b);if(a0 & b0)y=log(a)+log(b);if(a0 & b=0)y=sin(a)+sin(b);if(a=0)y=sin(a)+cos(b);printf(y=%fn,y);2-14 #includevoid main()int up,down,all,suit;scanf(%d%d,&up,&down);if(up=0)/*只买裤子*/all=down*45;if(down=0)/*只买上衣*/all=up*60;if(updown)if(down=50)all=down*80+(up-down)*60;elseal
10、l=down*90+(up-down)*60;elseif(up=50)all=up*80+(down-up)*60;elseall=up*90+(down-up)*60;printf(all=%dn,all);3-1 #includemain()long s=1;int i;for(i=1;i=10;i+)s=s*i;printf(s=%ldn,s);3-2 #includemain()double s=1;int i;for(i=1;i=100;i+)s=s*i;printf(s=%fn,s);3-3 #includemain()int i;for(i=1;i=100;i+)if(i%4=
11、0)printf(%dn,i);3-4 #includemain()int i=1;float s=0;while( 1.0/i=0.00001 )s=s+1.0/i;i+;printf(s=%fn,s);3-5 #include#includemain()float pi=0;int i=1;int t=1;while( 1.0/(2*i-1)=1e-6 )pi=pi+t*1.0/(2*i-1);t=-t;i+;pi=4*pi;printf(pi=%fn,pi);3-6 #includemain()float x;float r=0;float s=1;int t=1;int i;scanf
12、(%f,&x);for(i=1;i=20;i+)s=s*x;r=r+t*s/i;t=-t;printf(r=%fn,r);3-7 #includemain()long f1=1,f2=1;long f3;int i;printf(%d %d,f1,f2);for(i=3;i=40;i+)f3=f1+f2;f1=f2;f2=f3;printf(%ld ,f3);3-8 #includemain()int m;int i;scanf(%d,&m);for( i=2;im;i+ )if( m%i=0 )break;if( i=m )printf(%d is sushun,m);elseprintf(
13、%d is not sushun,m);3-9 #includemain()int m;int i;for( m=1;m=1000;m+ )for(i=2;im;i+)if( m%i=0 )break;if( i=m )printf(%dn,m);3-10 #includemain()int i,j;for(i=1;i=5;i+)for(j=1;j=5-i;j+)printf( );for(j=1;j=2*i-1;j+)printf(*);printf(n);for(i=1;i=4;i+)for(j=1;j=i;j+)printf( );for(j=1;j=9-2*i;j+)printf(*)
14、;printf(n);3-11 #includemain()int a20;int i;int max;for(i=0;i20;i+)scanf(%d,&ai);max=a0;for(i=1;i20;i+)if( maxai )max=ai;printf(max=%dn,max);3-12 #includemain()int m,n,t;int i;int max,min;scanf(%d%d,&m,&n);if( mn )t=m;m=n;n=t;for(i=1;i=m;i+)if( m%i=0 & n%i=0 )max=i;for(i=n;i=m*n;i+)if( i%m=0 & i%n=0
15、 )min=i;break;printf(max=%dnmin=%dn,max,min);3-13 #includemain()int x,y,z;for(x=0;x=50;x+)for(y=0;y=33;y+)for(z=0;z=100;z=z+2)if( 2*x+3*y+z/2=100 & x+y+z=100 )printf(x=%d,y=%d,z=%dn,x,y,z);3-16 #includemain()int x;long y=0;for(x=1;x=20;x+)y=y+2*x*x+3*x+1;printf(y=%ldn,y);3-17 #includemain()int y;for
16、( y=1;y=1000;y+ )if( (y%4=0) & (y%100!=0) | (y%400=0) )printf(%dn,y);3-18 #includemain()float people=13;float increase=0.008;int year=0;while( people=26 )people=people*(1+increase);year+;printf(year=%dn,year);3-19 #includemain()int i=1;float s=0;while( 1.0/(i*i)1e-6 )s=s+1.0/(i*i);i+;printf(s=%fn,s)
17、;3-20 #includemain()int i;float a=2;float s=0;for(i=0;i20;i+)s=s+a;a=1+1/a;printf(s=%fn,s);3-21 #includemain()int i=1;int m=1;float s=0;while( 1.0/i=1e-6 )s=s+1.0/i;i=i+m;m+;printf(s=%fn,s);3-22 #includemain()int n=1;float pi=2;while(n1000)pi=pi*(2*n*2*n)/(2*n-1)*(2*n+1);n+;printf(pi=%fn,pi);3-23 #i
18、ncludemain()float s=0;float s1=1;int n;for(n=1;n=10;n+)s1=s1*n;s=s+s1;printf(s=%fn,s);3-24 #includemain()int x;int n=1;float result=1;float s=1;float s2=1scanf(%d,&x);while(n100)s=s*n;s2=s2*x;result=result+s2/s;n+;printf(result=%fn,result);3-25 #includemain()int m;int a,b,c;for(m=100;m1000;m+)a=m/10
19、0;b=(m%100)/10;c=m%10;if(m=a*a*a+b*b*b+c*c*c)printf(%dn,m);3-26 #include void main()int n,a;int i;int b=0,s=0;scanf(%d%d,&n,&a);for( i=1;i=n;i+ )b=b*10+a;s=s+b;printf(s=%dn,s);3-27 #include void main()int i,j,s;for( i=1;i=1000;i+ )s=0;for( j=1;ji;j+ )if( i%j=0 )s=s+j;if( i=s )printf(%dn,i);3-28 #inc
20、ludevoid main()int i,j;for(i=1;i=5;i+)for(j=1;j=5-i;j+)printf( );for(j=1;j=9;j+)printf(*);printf(n);3-29 #include void main()int x,y,z;for( x=0;x=9;x+ )for( y=0;y=12;y+ )for( z=0;z=36;z=z+2 )if( (x+y+z)=36 & (4*x+3*y+z/2)=36 )printf(x=%d,y=%d,z=%dn,x,y,z);3-30 #includevoid main()int s=0;int x;int i;
21、float ave;for(i=1;i=20;i+)scanf(%d,&x);s=s+x;ave=float(s)/20;printf(s=%d,ave=%fn,s,ave);4-1 #includevoid main()int i,a10;for(i=0;i=0;i-)printf(%d ,ai);4-2 #includevoid main()double a50;int i;a0=1;a1=1;for(i=2;i50;i+)ai=ai-1+ai-2;for(i=0;i50;i+)printf(%.0f ,ai);4-3 #include #include #include void mai
22、n( ) int i,a100,max;srand(unsigned)time(NULL); for(i=0;i100;i+)ai=rand()%100;for(i=0;i100;i+)printf(%d ,ai);max=a0;for(i=0;i100;i+)if(maxai)max=ai;printf(nmax=%dn,max);4-4 #includevoid main()int a4,i;int num=0;for(i=0;i10;i+)scanf(%d,&ai);for(i=0;i10;i+)if(ai%4=0)printf(%dn,ai);num+;printf(num=%dn,n
23、um);4-5 #includemain()int a10;int i,j,t;for( i=0;i10;i+ )scanf(%d,&ai);printf(beforen);for( i=0;i10;i+ )printf(%d ,ai);printf(n);for( i=0;i9;i+ )for( j=0;jaj+1 )t=aj;aj=aj+1;aj+1=t;printf(aftern);for( i=0;i10;i+ )printf(%d ,ai);4-6 #includemain()int a10;int i,j,t,pos;for( i=0;i10;i+ )scanf(%d,&ai);p
24、rintf(beforen);for( i=0;i10;i+ )printf(%d ,ai);printf(n);for( i=0;i9;i+ )pos=i;for( j=i+1;jaj )pos=j;t=ai;ai=apos;apos=t;printf(aftern);for( i=0;i10;i+ )printf(%d ,ai);4-7 #includemain()int a33;int i,j;for( i=0;i3;i+ )for( j=0;j3;j+ )scanf(%d,&aij);for( i=0;i3;i+ )for( j=0;j3;j+ )printf(%d ,aij);pri
25、ntf(n);4-8 #includemain()int a33;int i,j;int sum=0;for( i=0;i3;i+ )for( j=0;j3;j+ )scanf(%d,&aij);for( i=0;i3;i+ )for( j=0;j3;j+ )printf(%d ,aij);printf(n);for( i=0;i3;i+ )for( j=0;jj )sum=sum+aij;printf(sum=%dn,sum);4-9 #includemain()int a1010=0;int i,j;a00=1;for( i=1;i10;i+ )ai0=1;for( j=1;j=i;j+
26、)aij=ai-1j+ai-1j-1;for( i=0;i10;i+ )for( j=0;j=i;j+ )printf(%d ,aij);printf(n);4-10 #includevoid main()int a33;int i,j,t;for( i=0;i3;i+ )for( j=0;j3;j+ )scanf(%d,&aij);printf(beforen);for( i=0;i3;i+ )for( j=0;j3;j+ )printf(%d ,aij);printf(n);for( i=0;i3;i+ )for( j=i;j3;j+ )t=aij;aij=aji;aji=t;printf
27、(aftern);for( i=0;i3;i+ )for( j=0;j3;j+ )printf(%d ,aij);printf(n);4-13#include #include #include void main( ) int a10;int i;int sum=0;float ave;srand(unsigned)time(NULL);for(i=0;i10;i+)ai=rand()%10; for(i=0;i10;i+)printf(%d ,ai); for(i=0;i10;i+)sum=sum+ai;ave=sum/10.0;printf(nsum=%d,ave=%fn,sum,ave
28、);4-14#include #include #include void main( ) int a10;int i;int sum=0;float ave;srand(unsigned)time(NULL);for(i=0;i10;i+)ai=rand()%10; for(i=0;i10;i+)printf(%d ,ai); for(i=0;i10;i+)if( ai%2=0 )sum=sum+ai;ave=sum/10.0;printf(nave=%fn,ave);4-15 #include #include #include void main( ) int a10;int i,t;s
29、rand(unsigned)time(NULL);for(i=0;i10;i+)ai=rand()%10; printf(beforen);for(i=0;i10;i+)printf(%d ,ai); for(i=0;i10/2;i+)t=ai;ai=a9-i;a9-i=t;printf(naftern);for(i=0;i10;i+)printf(%d ,ai); 4-16 #include #include #include void main( ) int a100;int i;int odd=0,even=0;srand(unsigned)time(NULL);for(i=0;i100
30、;i+)ai=rand()%10+1; for(i=0;i100;i+)printf(%d ,ai); for(i=0;i100;i+)if( ai%2=0 )even+;elseodd+;printf(neven=%d,odd=%dn,even,odd);4-17 #include #include #include void main( )int a100;int i;int num10=0;srand(unsigned)time(NULL);for(i=0;i100;i+)ai=rand()%10;for(i=0;i100;i+)printf(%d ,ai);for(i=0;i100;i
31、+)numai+;for(i=0;i10;i+)printf(nnum%d: %d,i,numi);4-18 #include #include #include void main( ) int a10,b10;int i;int sum=0;float ave;srand(unsigned)time(NULL);for(i=0;i10;i+)ai=rand()%10; printf(A:);for(i=0;i10;i+)printf(%d ,ai); for(i=0;i10;i+)bi=ai;printf(nB:);for(i=0;i10;i+)printf(%d ,bi); 4-19 #include #include #include void main( ) int a3=1,2,3,b7=2,3,4,5;int i;printf(beforen);printf(A:);for(i=0;i3;i+)printf(%d ,ai);printf(nB:);for(i=0;i4;i+)printf(%d ,bi);for(i=0;i3;i+)b4+i=ai