资源描述
浙江省秋季C语言二级考试上机编程试题及答案
1.三个数比较大小。
#include <stdio.h>
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(a>b) swap(&a,&b);
if(b>c) swap(&b,&c);
if(______2______) //a>b
swap(&a,&b);
printf("%d,%d,%d",a,b,c); }
2.体现式求和。
#include <stdio.h> #include <math.h>
void main()
{ FILE *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.141594
3.字母后移循环输出。
#include <stdio.h>
void main()
{ char c; c=getchar();
if(______1______) // c>='a' && c<'v'
c=c+5;
else
if (c>='v' && c<='z')
______2______ // c=c-21;
putchar(c); }
4.求满足条件旳数。
#include <stdio.h>
#include <math.h>
void main()
{ float y=1.05; int n=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,992137
5.求满足条件旳数。
#include <stdio.h>
void main()
{ int m=0,t=1,n;
while( _____ 1 ________); // (scanf("%d",&n),n<=0)
while(!(t<=n&&t*2>=n)){
_____ 2 _____ // t=t*2;
m++; }
printf("%d\n",m); }
6.求平面点间旳最短距离。
#include <stdio.h> #include <math.h>
#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(x[0],y[0],x[1],y[1]);
p=fopen("Design1.dat","w");
for(i=0;i<9;i++)
for(j=i+1;j<10;j++)
if((c=len(x[i],y[i],x[j],y[j]))<minc)
minc=c;
fprintf(p,"%f",minc);
fclose(p); }
运行成果:1.457944
7.Fibonacci数列求值问题。
#include <stdio.h>
_______1______ // long f(int n);
void main()
{ printf("%ld\n",f(30)); }
long f(int n)
{ if( ______2______ ) // n==1 || n==2
return 1;
else
return f(n-1)+f(n-2); }
运行成果:832040
8.多项式求和问题。
#include <stdio.h> #include <math.h>
void main()
{ FILE *p; int i; float x=1.279,t=1,y=0;
float a[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;i<10;i++)
{ t=t*x; y=y+t*a[i]; }
fprintf(p,"%f",y);
fclose(p); }
运行成果:98.722542
9.整数转换为字符串。
#include<stdio.h>
void itoa(long i,char *s)
{ if(i==0) return;
/****** 1 ******/
*s = '1'+i%10; //*s='0'+i%10
itoa(i/10,s-1); }
void main()
{ long n; char str[7]=""; scanf("%ld",&n);
/****** 2 ******/
itoa(n,str+6); // itoa(n,str+5);
printf("%s",str); }
10.Fibonacci数列求值问题。
#include<stdio.h>
void 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;i<40;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.020950
11.数组赋值。
#include <stdio.h>
void main()
{ int a[10],b[10],i;
printf("\ninput 10 numbers: ");
for (i=0; i<10;i++) /* 数组输入 */
scanf("%d", &a[i]);
for (i=1; i<10; i++)
b[i]=______1______; // b[i]=a[i]+a[i-1];
for (i=1; i<10; i++)
{ printf("%3d",b[i]);
if (______2______) printf("\n"); // i%3==0 } }
12.求各点距离和。
#include<stdio.h> #include<math.h>
void main()
{ FILE *p; int i;
float x[10]={-1.5,2.1,6.3,3.2,-0.7,7.0,5.1,3.2,4.5,7.6};
float y[10]={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;i<10;i++)
s=s+sqrt(pow(x[i]-1, 2)+pow(y[i]-1, 2));
fprintf(p,"%.6f",s); fclose(p); }
运行成果:52.679447
13.十进制数转换为二进制数。
#include <stdio.h>
void dec2bin(int m)
{ int bin[32],j;
for(j=0;m!=0;j++)
{ bin[j]= ______1______; // m%2
m=m/2; }
for(;j!=0;j--)
printf("%d", ______2______ ); // bin[j-1] }
void main()
{ int n;
scanf("%d",&n);
dec2bin(n); }
14.求符合条件旳数列之和。
#include <stdio.h>
#include <math.h>
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(p,"%.3f",s);
fclose(p); }
运行成果:121.336
15.在字符串中删除数字字符。
#include <stdio.h> #include <string.h> #include <ctype.h>
void f(char *s)
{ int i=0;
while(s[i]!='\0'){
if(isdigit(s[i])) ____1____(s+i,s+i+1); // strcpy
___2___ i++;} // else }
void main()
{ char str[80];
gets(str); f(str); puts(str); }
16.求满足条件旳数。
#include <stdio.h>
void 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;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, 6
17.清除数组中旳负数。
#include <stdio.h>
void f(int *a,int *m)
{ int i,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; } }
void main()
{ int i,n=7,x[7]={1,-2,3,4,-5,6,-7};
_______2_______; // f(x,&n);
for(i=0;i<n;i++) printf("%5d",x[i]);
printf("\n"); }
运行成果:1 3 4 6
18.二维数组中旳运算。
#include <stdio.h> #include <math.h>
void main()
{ float a[3][3]={{1.3,2.7,3.6},{2,3,4.7},{3,4,1.27}};
FILE *p; float x; int i,j;
for(i=0;i<3;i++)
{ x=fabs(a[i][0]);
for(j=1;j<3;j++)
if(fabs(a[i][j]>x)) x=fabs(a[i][j]);
for(j=0;j<3;j++)
a[i][j]=a[i][j]/x; }
p=fopen("Design2.dat","w");
for(i=0;i<3;i++) {
for(j=0;j<3;j++) fprintf(p,"%10.6f",a[i][j]);
fprintf(p,"\n"); }
fclose(p); }
运行成果:0.361111 0.750000 1.000000
0.425532 0.638298 1.000000
0.750000 1.000000 0.317500
19.平面上各点距离计算。
#include <stdio.h> #include <math.h> #include <stdlib.h>
void main()
{ int i,n;
/***** 1 *****/
struct axy { float x,y; } a; // struct axy{ float x; float y;} *a;
scanf("%d",&n);
a=(float*) malloc(n*2*sizeof(float));
for(i=0;i<n;i++)
/***** 2 *****/
scanf("%f%f",a[i].x,a[i].y); // scanf("%f%f",&a[i].x,&a[i].y);
for(i=0;i<n;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=(struct axy *) malloc(n*2*sizeof(float));
20.从a数组中找出偶数放入b数组。
#include <stdio.h>
void main()
{ FILE *p;
int i,j,temp,n=0;
int a[10]={7,6,20,3,14,88,53,62,10,29},b[10];
for(i=0;i<10;i++)
if(a[i]%2==0) b[n++]=a[i];
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(b[j]>b[j+1]) {temp=b[j];b[j]=b[j+1];b[j+1]=temp;}
p=fopen("Design1.dat","w");
for(i=0;i<n;i++)
{ fprintf(p,"%3d",b[i]);
if ( (i+1)%3==0) fputc (p, ‘\n’); }
fclose(p); }
运行成果:6 10 14
20 62 88
21.求输入整数旳各位数字之和。
#include <stdio.h> #include <math.h>
void main()
{ int n,s=0;
scanf("%d",&n);
______ 1 ______ // n=fabs(n);
while(n!=0) {
______ 2 ______ // s+=n%10;
n=n/10; }
printf("%d\n",s); }
22.有关生产能力旳数学应用题。
#include<stdio.h>
int 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");
fprintf(p,"%d,%d",year(20),year(30)); fclose(p); }
运行成果:,
23.穷举法求解方程。
#include <stdio.h>
void main()
{ FILE *p; int x,y,z,k=0;
p=fopen("Design1.dat","w");
for(x= -45;x<45;x++)
for(y= -45;y<45;y++)
for(z= -45;z<45;z++)
if(x*x+y*y+z*z==) k++;
fprintf(p,"%d",k); fclose(p); }
运行成果:144
24.字符串排序。
#include <stdio.h> #include <string.h>
void main()
{ FILE *p; char *s="634,.%@\\w|sq2",c;
int i,j,k,n=strlen(s);
p=fopen("Design2.dat","w");
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if( *(s+j)<*(s+i) ) {c=*(s+i); *(s+i)=*(s+j); *(s+j)=c; }
for(i=0;i<n;i++) fputc(s[i],p);
fclose(p); }
运行成果:%,.2346@\qsw|
25.将整数首尾倒置。
#include <stdio.h> #include <math.h>
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("%ld\t",f(12345)); printf("%ld\n",f(-34567)); }
运行成果:54321 -76543
26.求数组旳平均值,及与平均数旳差。
#include <stdio.h> #include <math.h>
void main()
{ FILE *p; int i,k=0;
float x[10]={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<10;i++) v+=x[i]; v=v/10; d=fabs(x[0]-v);
p=fopen("Design1.dat","w");
for(i=1;i<10;i++)
if(fabs(x[i]-v)<d){ d=fabs(x[i]-v); k=i;}
fprintf(p,”%.5f”,x[k]); fclose(p); }
运行成果:2.10000
27.求平方根数列之和。
#include <stdio.h> #include <math.h>
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, “%.10f\n”, s);
fclose(p); }
运行成果:21.
28.求多项式之和
#include <stdio.h>
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("%ld\n",s); }
29.计算学生旳平均成绩,并输出。
#include <stdio.h>
struct STUDENT
{ char name[16];
int math;
int english;
int computer;
int average; };
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 st[4]={{"Jessica",98,95,90},{"Mike",80,80,90},
{"Linda",87,76,70},{"Peter",90,100,99}};
for(i=0;i<4;i++)
{ GetAverage (____2________); // st+i }
printf("Name\tMath\tEnglish\tCompu\tAverage\n");
for(i=0;i<4;i++)
{ printf("%s\t%d\t%d\t%d\t%d\n",st[i].name,st[i].math,st[i].english,
st[i].computer,st[i].average); } }
30.求符合条件旳数。
#include <stdio.h> #include <math.h> #include <stdlib.h>
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); }
运行成果:313
31.求Armstrong数。
#include<stdio.h> #include<math.h>
void main()
{ int i, m,s=0;
printf("armstrong numbers in 100-999:");
for(i=100; i<1000; 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 407
32.将两个字符串连接起来。
#include <stdio.h>
void main()
{ char s1[80],s2[40];
int i=0,j=0;
printf("\ninput the first string:");
scanf("%s",s1);
printf("\ninput the second string:");
scanf("%s",s2);
while (s1[i] !='\0')
/****** 1 ******/
i+1; // i++;
while (s2[j] !='\0')
/****** 2 ******/
s1[++i]=s2[++j]; // s1[i++]=s2[j++]; /* 拼接字符到s1 */
s1[i] ='\0';
printf("\nnew string: %s",s1); }
33.选择法排序。
#include <stdio.h> #define N 10
void main()
{ int i,j,min,temp; int a[N]={5,4,3,2,1,9,8,7,6,0};
printf("\nThe array is:\n"); /* 输出数组元素 */
for (i=0;i<N;i++)
printf("%5d",a[i]);
for (i=0;i<N-1;i++) /* 排序操作 */
{ min = i;
for (j=i+1; j<N; j++)
/****** 1 ******/
if (a[min]<=a[j]) min =j; // if (a[min]>=a[j]) min =j;
/****** 2 ******/
temp=a[min]; a[min]=a[j]; a[j]=temp; /* 数据互换 */
// temp=a[min]; a[min]=a[i]; a[i]=temp; }
printf("\nThe sorted numbers: \n"); /* 输出排序成果 */
for (i=0;i<N;i++)
printf("%5d",a[i]);
printf("\n"); }
34.计算字符串中字符权重值。
#include<stdio.h> #include<math.h>
void main()
{ FILE *p; int i,w; char *s="we45*&y3r#$1";
p=fopen(“Design1.dat”,”w”);
for(i=0;s[i]!=’\0’;i++)
{ w=s[i]*(i+1);
fprintf(p,“%d”,w); }
fclose(p); }
运算成果:
35.将字符串中旳某个字符删除。
#include <stdio.h>
void main()
{ char s[80]; int i,j; gets(s);
for(i=j=0;______1______;i++) // s[i]!=’\0’
if(s[i] != 'c')
{ s[j]=s[i];
______2_____ //j++;
}
s[j]='\0'; puts(s); }
36.计算体现式值。
#include <stdio.h>
void main()
{ 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<stdio.h> #include<math.h>
void 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)+5*sin(y-2);
if(z<z1){z1=z;x1=x;y1=y;} }
fprintf(p,”%d,%d”,x1,y1);
fclose(p); }
运算成果:1, 7
38.计算亲密数对。
#include <stdio.h>
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,%6d\n",a,b); }
fclose(p); }
运行成果:
220, 284
284, 220
1184, 1210
1210, 1184
2620, 2924
2924, 2620
39.十进制转换为十六进制数。
# include <stdio.h> # include <string.h>
char trans(int x)
{if(x<10) 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)
{str[i]=trans(n%16);
/********2********/
n%=16; // n/=16;
i++;}
return i-1; }
void main()
{int i,k,n; char *str;
scanf("%d",&n); k=DtoH(n,str);
for (i=0;i<=k;i++) printf("%c",str[k-i]); }
40.将字符串中旳所有非英文字母删除后输出。
#include <stdio.h> #include <string.h>
void main()
{ char str[256];
int i,j,k=0,n;
gets(str); n=strlen(str);
for(i=0;i<n;i++)
/********1********/
if (tolower(str[i])<'a' || tolower(str[i])>'z') //if (tolower(str[i])>='a' &&tolower(str[i])<'=z')
{ /********2********/
str[n]=str[i]; n++; // str[k]=str[i]; k++; }
str[k]='\0';
printf("%s\n",str); }
题目有错,程序开头必须加上ctype.h
41.输出整数旳质数因子。
#include <stdio.h>
void main()
{ int n,i;
scanf("%d",&n);
/****** 1 ******/
i=1; // i=2;
while(n>1)
if(n%i= =0) { printf("%d\t",i); n/=i; }
else
/******** 2 *******/
n++; // i++; }
42.计算整数各位数字之和。
#include <stdio.h> #include <math.h>
void main()
{ int n,s=0;
scanf("%d",&n); n=fabs(n);
/******** 1 *******/
while(n>1) { // while(n!=0)
s=s+n%10;
/******** 2 ******/
n=n%10; // n=n/10; }
展开阅读全文