当前位置:文档之家› 计算机等级考试c语言

计算机等级考试c语言

1. /*求菲玻那契数列的前20项。

菲玻那契数列前2项分别是1,1,从第3项开始,各项分别是其前2项之和。

*/#include <stdio.h>void fibonacci(int f[]){ int i;/************Fill in the blanks************/for(i=2;i<20;i++)/************Fill in the blanks************/f[i]=f[i-1]+f[i-2];}main(){ int f[20]={1,1},i;fibonacci(f);printf("\n菲玻那契数列的前20项为: ");for(i=0;i<20;i++){if(i%5==0) printf("\n");/************Fill in the blanks************/printf("%12d",f[i]);}getch();}2. /*求广义菲玻那契数列的第n项。

广义菲玻那契数列前3项分别是1,1,1,从第4项开始,各项分别是其前3项之和。

广义菲玻那契级数的前n项为:1,1,1,3,5,9,17,31,……。

例如,若n = 20,则应输出广义菲玻那契级数的第20项为: 46499。

*/#include <stdio.h>long fibonacci(int n){ long a=1,b=1,c=1,d;int i;/************Fill in the blanks************/for(i=4;i<=n;i++){/************Fill in the blanks************/d=a+b+c;a=b;b=c;c=d;}/************Fill in the blanks************/return d;}main(){ int n=20;printf("广义菲玻那契级数的第%d项为: %ld\n",n,fibonacci(n));getch();}5. /*在sum函数中,根据整型形参m,计算如下公式的值。

1 1 1 1y = ------- + --------- + --------- + …… + -------100*100 200*200 300*300 m*m 例如,若形参m = 2000,则程序输出:The result is: 0.000160。

*/#include <stdio.h>/************Fill in the blanks************/double sum(int m){ int i;double y,d;/************Fill in the blanks************/y=0;for(i=100;i<=m;i+=100){d = (double)i * (double)i ;y += 1.0/d;}/************Fill in the blanks************/return(y);}main( ){ int n = 2000 ;printf("\nThe result is: %lf\n",sum(n));getch();}11. /*统计子字符串substr在字符串str中出现的次数。

例如,若字符串为This is a C Program,子字符串为is,则应输出2。

*/ #include <stdio.h>int count(char str[],char substr[]){ int i,j,k,num=0;/************Fill in the blanks************/for(i=0;str[i]!='\0';i++)for(j=i,k=0;substr[k]==str[j];k++,j++)/************Fill in the blanks************/if(substr[k+1]=='\0'){num++;break;}/************Fill in the blanks************/return num;}main(){char str[80],substr[80];printf("Input a string: ");gets(str);printf("Input a substring: ");gets(substr);printf("The result is: %d\n",count(str,substr));getch();}13. /*有一个3×4的矩阵,求所有元素中的最小值。

*/#include <stdio.h>min_value(int array[][4]){ int i,j,min;min=array[0][0];for(i=0;i<3;i++)/************Fill in the blanks************/for(j=0;j<4;j++)if(array[i][j]<min)/************Fill in the blanks************/min=array[i][j];/************Fill in the blanks************/return(min);}main(){ int a[3][4]={{-11,23,15,37},{29,48,6,-8},{15,17,34,12}}; printf("矩阵中所有元素的最小值=%d\n",min_value(a)); getch();}14. /*求班级学生考试成绩的平均值。

*/#include <stdio.h>float average(float array[],int n){ int i;float aver,sum=array[0];/************Fill in the blanks************/for(i=1;i<n;i++)sum=sum+array[i];/************Fill in the blanks************/aver=sum/n;/************Fill in the blanks************/return (aver);}main(){ float score_1[5]={98.5,97,91.5,60,55};float score_2[10]={67.5,89.5,99,69.5,77,89.5,76.5,54,60,99.5};printf("班级A学生考试成绩的平均值= %6.2f\n",average(score_1,5)); printf("班级A学生考试成绩的平均值= %6.2f\n",average(score_2,10)); getch();}17. /*求出以下分数序列的前n项之和。

2 3 5 8 13 21┄┄, ┄┄, ┄┄, ┄┄, ┄┄, ┄┄, ……1 2 3 5 8 13例如,若n = 5,则应输出:8.391667。

*/#include <stdio.h>/************Fill in the blanks************/double sum(int n){ int a,b,c,k;double s;/************Fill in the blanks************/s=0;a=2;b=1;for(k=1;k<=n;k++){s=s+(double)a/b;c=a;/************Fill in the blanks************/a=b+c;b=c;}return s;}main(){ int n=5;printf("\nThe value of function sum is: %lf\n",sum(n));getch();}20. /*输出M行M列整数方阵,并计算主对角线上各元素之和。

*/#include <stdio.h>#define M 5/************Fill in the blanks************/int count(int n,int a[M][M]){ int i,sum=0;for(i=0;i<n;i++)sum += a[i][i];/************Fill in the blanks************/return(sum);}main( ){ int arr[M][M]={{1,2,3,4,5},{4,3,2,1,0},{6,7,8,9,0},{9,8,7,6,5},{3,4,5,6,7}}; int i,j;printf("\n%d×%d 数组元素为:\n",M,M);for(i=0;i<M;i++ ){for(j=0;j<M;j++ )/************Fill in the blanks************/printf("%4d",arr[i][j]);printf("\n");}printf ( "主对角线上各元素之和为: %d",count(M,arr));getch();}24./*求出数组arr中的最大数,并把最大数和arr[0]中的数进行交换。

*/ #include<stdio.h>#define N 20/*************Fill in the blanks*************/Void swap(int a[],int n){int k,m,t;m=0;/*************Fill in the blanks************/for(k=0;k<n;k++)if(a[k]>a[m])m=k;t=a[0];/************Fill in the blanks************/a[0]=a[m];a[m]=t;}Main(){int I,n=10,arr[N]={0,5,12,10,23,6,9,7,10,8};printf(“\n交换前:”);For(i=0;i<n;i++)printf(“%4d”,arr[i]);Swap(arr,n);printf(“\n交换后:”);for(i=0;i<n;i++) printf(“%4d”,arr[i]);printf(“\n”);}3. /*将一个二维数组a的行和列元素互换,存到另一个二维数组b中。

相关主题