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,&a,&b,&c); if(ab) swap(&a,&b); if(bc) swap(&b,&c); if(_2_) /ab swap(&a,&b); printf(%d,%d,%d,a,b,c); 2体现式求和。#include #include void main() F
2、ILE *fp; float n=1,t=1,pi=0; int i; / 从如下开始答题 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字母后移循环输出。#include void main() char c; c=getchar(); if(_1_) / c=a & c=v & c=z) _2_ / c=c-21; putchar(c); 4求满足条件旳数。#include #inclu
3、de void main() float y=1.05; int n=1; FILE *p; / 如下开始做答 while(!(pow(y,n)1e6) n+; p=fopen(Design2.dat,w); fprintf(p,%d,%.0f,n,pow(1.05,n); fclose(p); 运行成果:283,9921375求满足条件旳数。#include void main() int m=0,t=1,n; while( _ 1 _); / (scanf(%d,&n),n=0) while(!(t=n) _ 2 _ / t=t*2; m+; printf(%dn,m); 6求平面点间旳最
4、短距离。#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.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;i9;i+) f
5、or(j=i+1;j10;j+) if(c=len(xi,yi,xj,yj)minc) minc=c; fprintf(p,%f,minc); fclose(p); 运行成果:1.4579447Fibonacci数列求值问题。#include _1_ / long f(int n);void main() printf(%ldn,f(30); long f(int n) if( _2_ ) / n=1 | n=2 return 1; else return f(n-1)+f(n-2); 运行成果:8320408多项式求和问题。#include #include void main() FILE
6、 *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;i10;i+) t=t*x; y=y+t*ai; fprintf(p,%f,y); fclose(p); 运行成果:98.7225429整数转换为字符串。#includevoid itoa(long i,char *s) if(i=0) return; /* 1 */ *s = 1+i%10;/*s=0+i%10 itoa(i/10,s
7、-1); void main() long n; char str7=; scanf(%ld,&n); /* 2 */ itoa(n,str+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;i40;i=i+2) t1=t1+t2; t2=t1+t2; f1=f1+f2; f2=f1
8、+f2; s=s+t1/f1+t2/f2; fprintf(p,%.6f,s); fclose(p); 运行成果:65.02095011数组赋值。#include void main() int a10,b10,i; printf(ninput 10 numbers: ); for (i=0; i10;i+) /* 数组输入 */ scanf(%d, &ai); for (i=1; i10; i+) bi=_1_; / bi=ai+ai-1; for (i=1; i10; i+) printf(%3d,bi); if (_2_) printf(n); / i%3=0 12求各点距离和。#inc
9、lude #includevoid main() FILE *p; int i; float 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;i10;i+) s=s+sqrt(pow(xi-1, 2)+pow(yi-1, 2); fprintf(p,%.6f,s); fclose(p); 运行成果:52.67944713十进制数转换为二进制数。#include
10、void dec2bin(int m) int bin32,j; for(j=0;m!=0;j+) binj= _1_; / m%2 m=m/2; for(;j!=0;j-) printf(%d, _2_ ); / binj-1 void main() int n; scanf(%d,&n); dec2bin(n); 14求符合条件旳数列之和。#include #include void main() FILE *p; float s=0,a=81;int i; p=fopen(Design2.dat,w); for(i=1;i=30;i+) s=s+a; a=sqrt(a); fprintf
11、(p,%.3f,s); fclose(p); 运行成果:121.33615在字符串中删除数字字符。#include #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 main() FILE *p; float f(float x,float y),min; int x,y
12、,x1,y1; p=fopen(Design1.dat,w); min=f(1,1); for(x=1;x=6;x+) for(y=1;y=6;y+) if (f(x,y)min) x1=x;y1=y;min=f(x,y); fprintf(p,%d,%d,x1,y1); fclose(p); float f(float u,float v) return (3.14*u-v)/(u+v); 运行成果:1, 617清除数组中旳负数。#include void f(int *a,int *m) int i,j; for(i=0;i*m;i+) if(ai0) for(j=i-;j*m-1;j+)
13、 aj=aj+1; _1_; / *m=*m-1; void main() int i,n=7,x7=1,-2,3,4,-5,6,-7; _2_; / f(x,&n); for(i=0;in;i+) printf(%5d,xi); printf(n); 运行成果:1 3 4 618二维数组中旳运算。#include #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;i3;i+) x=fabs(ai0); for(j=1;jx) x=fabs(aij);
14、for(j=0;j3;j+) aij=aij/x; p=fopen(Design2.dat,w); for(i=0;i3;i+) for(j=0;j3;j+) fprintf(p,%10.6f,aij); fprintf(p,n); fclose(p); 运行成果:0.361111 0.750000 1.000000 0.425532 0.638298 1.000000 0.750000 1.000000 0.31750019平面上各点距离计算。#include #include #include void main() int i,n; /* 1 */ struct axy float x,
15、y; a;/ struct axy float x; float y; *a; scanf(%d,&n); a=(float*) malloc(n*2*sizeof(float); for(i=0;in;i+) /* 2 */ scanf(%f%f,ai.x,ai.y); / scanf(%f%f,&ai.x,&ai.y); for(i=0;in;i+) if(sqrt(ai.x*ai.x+ai.y*ai.y)=5) printf(%f,%fn,ai.x,ai.y); *试题自身有错误,a=(struct axy *) malloc(n*2*sizeof(float);20从a数组中找出偶数放
16、入b数组。#include void 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;i10;i+) if(ai%2=0) bn+=ai; for(i=0;in-1;i+) for(j=0;jbj+1) temp=bj;bj=bj+1;bj+1=temp; p=fopen(Design1.dat,w); for(i=0;in;i+) fprintf(p,%3d,bi); if ( (i+1)%3=0) fputc (p, n); fclose(p); 运行成果:6 10 14 2
17、0 62 8821求输入整数旳各位数字之和。 #include #include void main() int n,s=0; scanf(%d,&n); _ 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(p=x) p=p*(1+0.098); y+; return y; void main() FILE *p; p=fopen(design.dat,w); fpr
18、intf(p,%d,%d,year(20),year(30); fclose(p); 运行成果:2023,202323穷举法求解方程。#include void main() FILE *p; int x,y,z,k=0; p=fopen(Design1.dat,w); for(x= -45;x45;x+) for(y= -45;y45;y+) for(z= -45;z45;z+) if(x*x+y*y+z*z=2023) k+; fprintf(p,%d,k); fclose(p); 运行成果:14424字符串排序。#include #include void main() FILE *p;
19、 char *s=634,.%w|sq2,c; int i,j,k,n=strlen(s); p=fopen(Design2.dat,w); for(i=0;in-1;i+) for(j=i+1;jn;j+) if( *(s+j)*(s+i) ) c=*(s+i); *(s+i)=*(s+j); *(s+j)=c; for(i=0;in;i+) fputc(si,p); fclose(p); 运行成果:%,.2346qsw|25将整数首尾倒置。#include #include long f(long n) long m,y=0; m=fabs(n); while(m!=0) y=y*10+m
20、%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求数组旳平均值,及与平均数旳差。#include #include void main() 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;i10;i+) v+=xi; v=v/10; d=fabs(x
21、0-v);p=fopen(Design1.dat,w); for(i=1;i10;i+) if(fabs(xi-v)d) d=fabs(xi-v); k=i; fprintf(p,”%.5f”,xk); fclose(p); 运行成果:2.1000027求平方根数列之和。#include #include void main() FILE *p; int i; double s=0; for(i=2;i=10;i+) s+=sqrt(i); p=fopen(“design2.dat”,”w”); fprintf ( p, “%.10fn”, s); fclose(p); 运行成果:21.28求
22、多项式之和#include void main() int i,a,n; long t=0; /* 1 */ s=0;/ long s=0; scanf(%d%d,&a,&n); for(i=1;i=n;i+) /* 2 */ t=t*10+i;/ t=t*10+1 s=s+t; s=s*a; printf(%ldn,s); 29计算学生旳平均成绩,并输出。#include struct STUDENT char name16; int math; int english; int computer; int average; ;void GetAverage(struct STUDENT *
23、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;i4;i+) GetAverage (_2_); / st+i printf(NametMathtEnglishtComputAveragen); for(i=0;i4;i+) p
24、rintf(%st%dt%dt%dt%dn,sti.name,sti.math,sti.english, sti puter,sti.average); 30求符合条件旳数。#include #include #include void main( ) FILE *p; int i,j; (p=fopen(design.dat,w); for(i=1; ; i+) if(i%3=1&i%5=3&i%7=5&i%9=7) break; fprintf(p,%d,i); fclose(p); 运行成果:31331求Armstrong数。#include #includevoid main() in
25、t i, m,s=0;printf(armstrong numbers in 100-999:);for(i=100; i1000; i+)m=i; s=0; while (m!=0) s+=pow(m%10,3); m=m/10; if(s= =i) printf(%5d, i); 运行成果:153 370 371 40732将两个字符串连接起来。#include void main() char s180,s240; int i=0,j=0; printf(ninput the first string:); scanf(%s,s1); printf(ninput the second s
26、tring:); 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选择法排序。#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;iN;i+) prin
27、tf(%5d,ai); for (i=0;iN-1;i+) /* 排序操作 */ min = i; for (j=i+1; jN; j+) /* 1 */ if (amin=aj) min =j; /* 2 */ temp=amin; amin=aj; aj=temp; /* 数据互换 */ temp=amin; amin=ai; ai=temp; printf(nThe sorted numbers: n); /* 输出排序成果 */ for (i=0;iN;i+) printf(%5d,ai); printf(n); 34计算字符串中字符权重值。#include #includevoid
28、main() FILE *p; int i,w; char *s=we45*&y3r#$1; p=fopen(“Design1.dat”,”w”); for(i=0;si!=0;i+) w=si*(i+1); fprintf(p,“%d”,w); fclose(p); 运算成果:8835将字符串中旳某个字符删除。#include void main() char s80; int i,j; gets(s); for(i=j=0;_1_;i+) / si!=0 if(si != c) sj=si; _2_ /j+; sj=0; puts(s); 36计算体现式值。#include void ma
29、in() FILE *p; long s=1,k=1; int i;for(i=2;i=12;i+) k*=i; s+=k; p=fopen(“Design2.dat”,”w”); fprintf(p,”%ld”,s); fclose(p); 运算成果:37求满足体现式规定旳最小值。#include #includevoid main() FILE *p; int x,y,x1,y1; float z,z1;p=fopen(Design1.dat,w);z1=10*cos(0-4)+5*sin(0-2); for(x=0;x=10;x+)for(y=0;y=10;y+)z=10*cos(x-4
30、)+5*sin(y-2);if(zz1)z1=z;x1=x;y1=y; fprintf(p,”%d,%d”,x1,y1);fclose(p); 运算成果:1, 738计算亲密数对。#include void main() FILE *p; int a,b,c,k; p=fopen(Design1.dat,w); for(a=6;a=5000;a+) for(k=1,b=0;k=a/2;k+) if(a%k= =0) b+=k; for(k=1,c=0;k=b/2;k+) if(b%k= =0) c+=k; if(a= =c&a!=b) fprintf(p,%6d,%6dn,a,b); fclo
31、se(p); 运行成果:220, 284 284, 220 1184, 1210 1210, 1184 2620, 2924 2924, 262039十进制转换为十六进制数。# include # include char trans(int x)if(x10) return 0+x;/*1*/else return a+x; / else return a+(x-10); int DtoH(int n,char *str)int i=0;while(n!=0)stri=trans(n%16); /*2*/n%=16; / n/=16;i+;return i-1; void main()int
32、 i,k,n; char *str;scanf(%d,&n); k=DtoH(n,str);for (i=0;i=k;i+) printf(%c,strk-i); 40将字符串中旳所有非英文字母删除后输出。#include #include void main() char str256; int i,j,k=0,n; gets(str); n=strlen(str); for(i=0;in;i+) /*1*/if (tolower(stri)z) /if (tolower(stri)=a &tolower(stri)=z) /*2*/ strn=stri; n+; / strk=stri;
33、k+; strk=0; printf(%sn,str); 题目有错,程序开头必须加上ctype.h 41输出整数旳质数因子。#include void main() int n,i; scanf(%d,&n); /* 1 */ i=1; / i=2; while(n1) if(n%i= =0) printf(%dt,i); n/=i; else /* 2 */ n+; / i+; 42计算整数各位数字之和。#include #include void main() int n,s=0; scanf(%d,&n); n=fabs(n); /* 1 */ while(n1) / while(n!=0) s=s+n%10; /* 2 */ n=n%10; / n=n/10; printf(%dn,s); 43在字符串中查找单词。#include in