1、1二级考试(C 语言)上机试题1三个数比较大小。#include void swap(_1_) /int *pa,int *pb /*交换两个数的位置*/int temp; temp = *pa; *pa = *pb; *pb = temp; void main() int a,b,c,temp;scanf(“%d%d%d“,if(ab) swap(if(bc) swap(if(_2_) /abswap(printf(“%d,%d,%d“,a,b,c); 2表达式求和。#include #include void main() FILE *fp;float n=1,t=1,pi=0;int i
2、;/ 从以下开始答题 i=1;while(fabs(t)=1e-6) pi=pi+t; i=-i; n=n+2; t=i/n; fp=fopen(“Design1.dat“,“w“);fprintf(fp,“%.6f“,4*pi);fclose(fp); 运行结果:3.1415943字母后移循环输出。2#include void main() char c; c=getchar();if(_1_) / c=a int n=1; FILE *p;/ 以下开始做答while(!(pow(y,n)1e6) ?n+;p=fopen(“Design2.dat“,“w“);fprintf(p,“%d,%.
3、0f“,n,pow(1.05,n);fclose(p); 运行结果:283,9921375求满足条件的数。#include void main() int m=0,t=1,n;while( _ 1 _); / (scanf(“%d“,m+; printf(“%dn“,m); 6求平面点间的最短距离。3#include #include #define len(x1,y1,x2,y2) sqrt(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)void main() FILE *p; int i,j; float c,minc;float x=1.1,3.2,-2.5,5.67,3.
4、42,-4.5,2.54,5.6,0.97,4.65;float y=-6,4.3,4.5,3.67,2.42,2.54,5.6,-0.97,4.65,-3.33;minc=len(x0,y0,x1,y1);p=fopen(“Design1.dat“,“w“);for(i=0;i_1_ / long f(int n);void main() printf(“%ldn“,f(30); long f(int n) if( _2_ ) / n=1 | n=2return 1;elsereturn f(n-1)+f(n-2); 运行结果:8320408多项式求和问题。#include #include
5、 void main() FILE *p; int i; float x=1.279,t=1,y=0;float a10=1.1,3.2,-2.5,5.67,3.42,-4.5,2.54,5.6,0.97,4.65;p=fopen(“Design2.dat“,“w“);y=a0 ; for(i=1;ivoid itoa(long i,char *s) if(i=0) return;/* 1 */*s = 1+i%10; /*s=0+i%10itoa(i/10,s-1); void main() long n; char str7=“; scanf(“%ld“,/* 2 */itoa(n,str
6、+6); / itoa(n,str+5);printf(“%s“,str); 10Fibonacci 数列求值问题。 #includevoid main() FILE *p; int i; float f1=1.0,f2=2.0,t1=2.0,t2=3.0,s; float f,t;s=t1/f1+t2/f2; p=fopen(“Design1.dat“,“w“);for(i=3;ivoid main()5 int a10,b10,i;printf(“ninput 10 numbers: “);for (i=0; i #includevoid main() FILE *p; int i;flo
7、at x10=-1.5,2.1,6.3,3.2,-0.7,7.0,5.1,3.2,4.5,7.6;float y10=3.5,7.6,8.1,4.5,6.0,1.1,1.2,2.1,3.3,4.4; float s=0.0;p=fopen(“Design2.dat“,“w“);for(i=0;ivoid dec2bin(int m) int bin32,j;for(j=0;m!=0;j+) binj= _1_; / m%2m=m/2; for(;j!=0;j-)printf(“%d“, _2_ ); / binj-1 void main() int n;scanf(“%d“,dec2bin(n
8、); 14求符合条件的数列之和。6#include #include void main() FILE *p; float s=0,a=81;int i;p=fopen(“Design2.dat“,“w“);for(i=1;i #include #include void f(char *s) int i=0;while(si!=0)if(isdigit(si) _1_(s+i,s+i+1); / strcpy_2_ i+; / else void main() char str80; gets(str); f(str); puts(str); 16求满足条件的数。#include void
9、main() FILE *p; float f(float x,float y),min;int x,y,x1,y1;p=fopen(“Design1.dat“,“w“);min=f(1,1);for(x=1;xvoid f(int *a,int *m) int i,j;for(i=0;i #include void main() float a33=1.3,2.7,3.6,2,3,4.7,3,4,1.27;FILE *p; float x; int i,j;for(i=0;ix) x=fabs(aij);for(j=0;j #include #include void main() int
10、i,n;/* 1 */struct axy float x,y; a; / struct axy float x; float y; *a;scanf(“%d“,a=(float*) malloc(n*2*sizeof(float);for(i=0;ivoid main() FILE *p;int i,j,temp,n=0;int a10=7,6,20,3,14,88,53,62,10,29,b10;for(i=0;ibj+1) temp=bj;bj=bj+1;bj+1=temp;p=fopen(“Design1.dat“,“w“);for(i=0;i #include void main()
11、 int n,s=0;scanf(“%d“,_ 1 _ / n=fabs(n);while(n!=0) _ 2 _ / s+=n%10;n=n/10; printf(“%dn“,s); 22关于生产能力的数学应用题。#includeint year(int x) float p=11.5; int y=1999;while(pvoid main() FILE *p; int x,y,z,k=0;p=fopen(“Design1.dat“,“w“);for(x= -45;x #include void main() FILE *p; char *s=“634,.%w|sq2“,c;int i,j
12、,k,n=strlen(s);p=fopen(“Design2.dat“,“w“);for(i=0;i #include long f(long n) long m,y=0; m=fabs(n);while(m!=0) y=y*10+m%10; _1_ / m/=10; if(n=0) return y;else _2_ / return y; void main() printf(“%ldt“,f(12345); printf(“%ldn“,f(-34567); 运行结果:54321 -7654326求数组的平均值,及与平均数的差。11#include #include void main(
13、) FILE *p; int i,k=0;float x10=7.23,-1.5,5.24,2.1,-12.45,6.3,-5,3.2,-0.7,9.81,d,v=0;for(i=0;i #include void main() FILE *p;int i; double s=0;for(i=2;ivoid main() int i,a,n; long t=0;/* 1 */s=0; / long s=0;scanf(“%d%d“,for(i=1;istruct STUDENT char name16;int math;int english;int computer;int average;
14、 ;void GetAverage(struct STUDENT *pst) /* 计算平均成绩 */ int sum=0;sum = _1_ /sum+pst-math+pst-english+pst-computer;pst-average = sum/3; void main() int i;struct STUDENT st4=“Jessica“,98,95,90,“Mike“,80,80,90,“Linda“,87,76,70,“Peter“,90,100,99;for(i=0;i #include #include void main( ) FILE *p; int i,j;(p=
15、fopen(“design.dat“,“w“);for(i=1; ; i+)if(i%3=1fprintf(p,“%d“,i);fclose(p); 运行结果:31331求 Armstrong 数。13#include #includevoid main() int i, m,s=0;printf(“armstrong numbers in 100-999:“);for(i=100; ivoid main() char s180,s240;int i=0,j=0;printf(“ninput the first string:“);scanf(“%s“,s1);printf(“ninput t
16、he second string:“);scanf(“%s“,s2);while (s1i !=0)/* 1 */i+1; / i+;while (s2j !=0)/* 2 */s1+i=s2+j; / s1i+=s2j+; /* 拼接字符到 s1 */s1i =0;printf(“nnew string: %s“,s1); 33选择法排序。14#include #define N 10void main() int i,j,min,temp; int aN=5,4,3,2,1,9,8,7,6,0;printf(“nThe array is:n“); /* 输出数组元素 */for (i=0;
17、i=aj) min =j; /* 2 */temp=amin; amin=aj; aj=temp; /* 数据交换 */ temp=amin; amin=ai; ai=temp; printf(“nThe sorted numbers: n“); /* 输出排序结果 */for (i=0;i #includevoid main() FILE *p; int i,w; char *s=“we45*p=fopen(“Design1.dat”,”w”);for(i=0;si!=0;i+) w=si*(i+1);fprintf(p,“%d”,w); fclose(p); 运算结果:1192021562
18、12210228847408102635039658835将字符串中的某个字符删除。#include void main() char s80; int i,j; gets(s);for(i=j=0;_1_;i+) / si!=0if(si != c) sj=si;15_2_ /j+;sj=0; puts(s); 36计算表达式值。#include void main() FILE *p; long s=1,k=1; int i;for(i=2;i #includevoid main() FILE *p;int x,y,x1,y1; float z,z1;p=fopen(“Design1.da
19、t“,“w“);z1=10*cos(0-4)+5*sin(0-2);for(x=0;x16void main() FILE *p; int a,b,c,k;p=fopen(“Design1.dat“,“w“);for(a=6;a # include char trans(int x)if(x #include void main() char str256;int i,j,k=0,n;gets(str); n=strlen(str);for(i=0;iz) /if (tolower(stri)=a scanf(“%d“,/* 1 */i=1; / i=2;while(n1)if(n%i= =0
20、) printf(“%dt“,i); n/=i; else/* 2 */n+; / i+; 42计算整数各位数字之和。#include #include void main() int n,s=0;scanf(“%d“, n=fabs(n);18/* 1 */while(n1) / while(n!=0)s=s+n%10;/* 2 */n=n%10; / n=n/10; printf(“%dn“,s); 43在字符串中查找单词。#include int find(char *str) char *fstr=“the“; int i=0,j,n=0; while (stri!=0)for(_1_
21、) / j=0;fstrj!=0;j+if (strj+i!=fstrj) break;if (_2_) n+; / fstrj=0i+; return n; void main() char a80; gets(a); printf(“%d“,find(a); 44逆序连接字符串。#include #include void fun(char *s,char *t) int i,sl;sl=strlen(s);for(i=0;i #include void main() char a80; int n3=0,i; gets(a);_1_ / for(i=0;ai!=0;i+)if (tolo
22、wer(ai)=a float s=1,t=1,i=3;p=fopen(“design1.dat“,“w“);while(fabs(1/t) = 1e-5)t=-t* (i-1)*i; s=s+1/t; i+=2; fprintf(p,“%.6f“,s); fclose(p); 运行结果:0.841471 47计算多项式之和。#include float f(float,float*,int);void main() float b5=1.1,2.2,3.3,4.4,5.5; printf(“%fn“,f(1.7,b,5); float f( _1_ ) / float x, float *a
23、, int n float y=a0,t=1; int i;for(i=1;ivoid main() FILE *p; int i;float a10=7.23,1.5,5.24,2.1,2.45,6.3,5,3.2,0.7,9.81,sum,v=0;p=fopen(“Design2.dat“,“w“);for(i=0;i=v) sum=sum+ai;fprintf(p ,“ %.5f“, sum); fclose(p); 运行结果:33.5800049统计在圆内的点的个数。#include #include void main() FILE *p; int i,k=0; float x=1.
24、1,3.2,-2.5,5.67,3.42,-4.5,2.54,5.6,0.97,4.65;float y=-6,4.3,4.5,3.67,2.42,2.54,5.6,-0.97,4.65,-3.33;for(i=0;iint cnt,sum;void JSValue() int i,j,half;for(i=500;ihalf) cnt+;sum+=i; void main() FILE *p;p=fopen(“design.dat“,“w“); JSValue(); fprintf(p,“%d,%d“,cnt,sum); fclose(p); 运行结果:44,2854251求 Fibonac
25、ci 数列问题。#includeint JSValue(int t) int F40;int i;F0=0;F1=1;for(i=2;i+) Fi=Fi-1+Fi-2;if(Fit) break; return Fi; void main() FILE *p; p=fopen(“design.dat“,“w“);fprintf(p,“%d,%d“,JSValue(1000),JSValue(3000);fclose(p); 运行结果:1597, 418152求最大值、最小值问题。#include void main() float x,max,min; int i;/* 1 */for(i=0
26、;imax) max=x; if(xt | f2t) break;if (f1t if (f1t) return f2;22#include void main() int m,n,k;while(scanf(“%d%d“, / n #include void main() float x,eps,s=1,t=1,i=1;scanf(“%f%f“,do /* 1 */t=-t*x/+i; / t=-t*x/i+;s+=t;/* 2 */ while(fabs(t)void main() FILE *p; int s=0,i=0;char *a=“r235%34cdDW,.“;p=fopen(“
27、Design2.dat”,”w”);for(i=0;ai!=0;i+)s+=ai;fprintf(p,”%d”,s); fclose(p); 56矩阵计算问题。23#include void main() float a33=1.3,2.7,3.6,2,3,4.7,3,4,1.27; FILE *p;p=fopen(“Design1.dat”,”w”);for(i=0;ivoid main() FILE *p;int cock,hen,chick; p=fopen(“Design2,dat”,”w”);for(cock=0;cock=20;cock+) /*公鸡的数目作为外层循环的循环变量 */for(hen=0;hen=33;hen+) /*母鸡的数目作为内层循环的循环变量*/ chick=100-cock-hen; /*用总鸡数方程计算小鸡的数目 */if(chick%3=0 fclose(p);