您好,欢迎访问三七文档
当前位置:首页 > 办公文档 > 其它办公文档 > 浙江省计算机等级二级考试-C语言-机考题库
二级考试(C语言)上机部分试题1.三个数比较大小。输入三个整数,按由大到小的顺序输出这三个数。#includestdio.hvoidswap(int*pa,int*pb){/*交换两个数的位置*/inttemp;temp=*pa;*pa=*pb;*pb=temp;}voidmain(){inta,b,c,temp;scanf(%d%d%d,&a,&b,&c);if(ab)swap(&a,&b);if(bc)swap(&b,&c);if(ab)swap(&a,&b);printf(%d,%d,%d,a,b,c);}2.表达式求和。将计算结果以格式“%。6f”写入到考生文件夹中#includestdio.h#includemath.hvoidmain(){FILE*fp;floatn=1,t=1,pi=0;inti;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.字母后移循环输出。输入的一个小写字母,将字母循环后移5个位置后输出。#includestdio.hvoidmain(){charc;c=getchar();If(c='a'&&c'v')c=c+5;elseif(c='v'&&c='z')c=c-21;putchar(c);}4.求满足条件的数。#includestdio.h#includemath.hvoidmain(){floaty=1.05;intn=1;FILE*p;while(!(pow(y,n)1e6&&pow(y,n+1)1e6))n++;p=fopen(Design2.dat,w);fprintf(p,%d,%.0f,n,pow(1.05,n));fclose(p);}运行结果:283,9921375.求满足条件的数。输入整数n(n0),求m使得2的m次方小于或等于n,#includestdio.hvoidmain(){intm=0,t=1,n;while(scanf(%d,&n),n=0);while(!(t=n&&t*2=n)){t=t*2;m++;}printf(%d\n,m);}6.求平面点间的最短距离。数组元素x[i]#includestdio.h#includemath.h#definelen(x1,y1,x2,y2)sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))voidmain(){FILE*p;inti,j;floatc,minc;floatx[]={1.1,3.2,-2.5,5.67,3.42,-4.5,2.54,5.6,0.97,4.65};floaty[]={-6,4.3,4.5,3.67,2.42,2.54,5.6,-0.97,4.65,-3.33};minc=len(x[0],y[0],x[1],y[1]);p=fopen(Design1.dat,w);for(i=0;i9;i++)for(j=i+1;j10;j++)if((c=len(x[i],y[i],x[j],y[j]))minc)minc=c;fprintf(p,%f,minc);fclose(p);}运行结果:1.4579447.Fibonacci数列求值问题。数列的第1此后各项值均为该项前二项之和。#includestdio.hlongf(intn);voidmain(){printf(%ld\n,f(30));}longf(intn){if(n==1||n==2)return1;elsereturnf(n-1)+f(n-2);}运行结果:8320408.多项式求和问题。计算多项式#includestdio.h#includemath.hvoidmain(){FILE*p;inti;floatx=1.279,t=1,y=0;floata[10]={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=a[0];for(i=1;i10;i++){t=t*x;y=y+t*a[i];}fprintf(p,%f,y);fclose(p);}运行结果:98.7225429.整数转换为字符串。用递归法将一个六位整数转换成字符串。#includestdio.hvoiditoa(longi,char*s){if(i==0)return;/******1******/*s='1'+i%10;//*s='0'+i%10itoa(i/10,s-1);}voidmain(){longn;charstr[7]=;scanf(%ld,&n);/******2******/itoa(n,str+6);//itoa(n,str+5);printf(%s,str);}10.Fibonacci数列求值问题。有数列#includestdio.hvoidmain(){FILE*p;inti;floatf1=1.0,f2=2.0,t1=2.0,t2=3.0,s;floatf,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+f2;s=s+t1/f1+t2/f2;}fprintf(p,%.6f,s);fclose(p);}运行结果:65.02095011.数组赋值。#includestdio.hvoidmain(){inta[10],b[10],i;printf(\ninput10numbers:);for(i=0;i10;i++)/*数组输入*/scanf(%d,&a[i]);for(i=1;i10;i++)b[i]=b[i]=a[i]+a[i-1];for(i=1;i10;i++){printf(%3d,b[i]);if(i%3==0)printf(\n);}}12.求各点距离和。#includestdio.h#includemath.hvoidmain(){FILE*p;inti;floatx[10]={-1.5,2.1,6.3,3.2,-0.7,7.0,5.1,3.2,4.5,7.6};floaty[10]={3.5,7.6,8.1,4.5,6.0,1.1,1.2,2.1,3.3,4.4};floats=0.0;p=fopen(Design2.dat,w);for(i=0;i10;i++)s=s+sqrt(pow(x[i]-1,2)+pow(y[i]-1,2));fprintf(p,%.6f,s);fclose(p);}运行结果:52.67944713.十进制数转换为二进制数。#includestdio.hvoiddec2bin(intm){intbin[32],j;for(j=0;m!=0;j++){bin[j]=m%2;m=m/2;}for(;j!=0;j--)printf(%d,bin[j-1]);}voidmain(){intn;scanf(%d,&n);dec2bin(n);}14.求符合条件的数列之和。#includestdio.h#includemath.hvoidmain(){FILE*p;floats=0,a=81;inti;p=fopen(Design2.dat,w);for(i=1;i=30;i++){s=s+a;a=sqrt(a);}fprintf(p,%.3f,s);fclose(p);}运行结果:121.33615.在字符串中删除数字字符。#includestdio.h#includestring.h#includectype.hvoidf(char*s){inti=0;while(s[i]!='\0'){if(isdigit(s[i]))____1____(s+i,s+i+1);//strcpy___2___i++;}//else}voidmain(){charstr[80];gets(str);f(str);puts(str);}16.求满足条件的数。#includestdio.hvoidmain(){FILE*p;floatf(floatx,floaty),min;intx,y,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);}floatf(floatu,floatv){return(3.14*u-v)/(u+v);}运行结果:1,617.去除数组中的负数。#includestdio.hvoidf(int*a,int*m){inti,j;for(i=0;i*m;i++)if(a[i]0){for(j=i--;j*m-1;j++)a[j]=a[j+1];_____1_____;//*m=*m-1;}}voidmain(){inti,n=7,x[7]={1,-2,3,4,-5,6,-7};_______2_______;//f(x,&n);for(i=0;in;i++)printf(%5d,x[i]);printf(\n);}运行结果:134618.二维数组中的运算。#includestdio.h#includemath.hvoidmain(){floata[3][3]={{1.3,2.7,3.6},{2,3,4.7},{3,4,1.27}};FILE*p;floatx;inti,j;for(i=0;i3;i++){x=fabs(a[i][0]);for(j=1;j3;j++)if(fabs(a[i][j]x))x=fabs(a[i][j]);for(j=0;j3;j++)a[i][j]=a[i][j]/x;}p=fopen(Design2.dat,w);for(i=0;i3;i++){for(j=0;j3;j++)fprintf(p,%10.6f,a[i][j]);fprintf(p,\n);}fclose(p);}运行结果:0.3611110.7500001.0000000.4255320.6382981.0000000.7500001.0000000.31750019.平面上各点距离计算。#includestdio.h#includemath.h#includestdlib.hvoidmain(){inti,n;/*****1*****/structaxy{floatx,y;}a;//structaxy{floatx;floaty;}*a;scanf(%d,&n);a=(float*)malloc(n*2*sizeof(float));for(i=0;in;i++)/*****2*****/scanf(%f%f,a[i].x,a[i].y);//scanf(%f%f,&a[i].x,&a[i].y);for(i=0;in;i++)if(sqrt(a[i].x*a[i].x+a[i].y*a[i].y)=5)printf(%f,%f\n,a[i].x,a[i].y);}**试题本身有错误,a=(structaxy*)malloc(n*2*sizeof(float));20.从a数组中找出偶数放入b数组。#includestdio.hvoidmain(){FILE*p;inti,j,temp,n=0;inta[10]={7,6,20,3,14,88,53,62,10,29},b[10];for(i=0;i10;i++)if(a[i]
本文标题:浙江省计算机等级二级考试-C语言-机考题库
链接地址:https://www.777doc.com/doc-5305943 .html