首页 二级c语言上机试题总结(数学计算)

二级c语言上机试题总结(数学计算)

举报
开通vip

二级c语言上机试题总结(数学计算)1.1​ 实型四舍五入 请编一个函数fun,函数的功能是使实型数保留2位小数,并对第三位进行四舍五入 (规定实型数为正数)。 例如:实型数为 1234.567, 则函数返回 1234.57; 实型数为 1234.564, 则函数返回 1234.56。 注意: 部分源程序存在文件PROG1.C文件中。 请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 #include int NONO(void); float fun ( float h ) { } int main(v...

二级c语言上机试题总结(数学计算)
1.1​ 实型四舍五入 请编一个函数fun,函数的功能是使实型数保留2位小数,并对第三位进行四舍五入 (规定实型数为正数)。 例如:实型数为 1234.567, 则函数返回 1234.57; 实型数为 1234.564, 则函数返回 1234.56。 注意: 部分源程序存在文件PROG1.C文件中。 请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 #include int NONO(void); float fun ( float h ) { } int main(void) { float a; printf ("Enter a: "); scanf ( "%f", &a ); printf ( "The original data is : " ); printf ( "%f \n\n", a ); printf ( "The result : %f\n", fun ( a ) ); NONO( ); return 1; } int NONO(void) {/* 请在此函数内打开文件,输入测试数据,调用 fun 函数, 输出数据,关闭文件。 */ int i ; float a ; FILE *rf, *wf ; rf = fopen("./05/in.dat","r") ; wf = fopen("./05/out.dat","w") ; for(i = 0 ; i < 20 ; i++) { fscanf(rf, "%f", &a) ; fprintf(wf, "%f\n", fun(a)) ; } fclose(rf) ; fclose(wf) ; return 1; } 答案1: long i=h*1000; if(i%10<5) return (i/10)/100.0; else return (i/10+1)/100.0; 答案2: long temp=a*1000,temp2; temp2=temp/10; temp=temp%10; float result; //被除数定义成float if(temp>4) result=temp2+1; else result=temp2; return result/100; 1.2​ 根据公式求圆周率 #include #include double fun ( double eps) { } main( ) { double x; printf("Input eps:") ; scanf("%lf",&x); printf("\neps = %lf, PI=%lf\n", x, fun(x)); NONO(); } NONO ( ) {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *fp, *wf ; int i ; double x ; fp = fopen("c:\\test\\in.dat","r") ; wf = fopen("c:\\test\\out.dat","w") ; for(i = 0 ; i < 10 ; i++) { fscanf(fp, "%lf", &x) ; fprintf(wf, "%lf\n", fun(x)) ; } fclose(fp) ; fclose(wf) ; } 答案1: double i=1,a=1,b=1,c=1,s=0; while(c>=eps) { s+=c; a*=i; b*=2*i+1; c=a/b; i++; } return s*2; 答案2: double n=1.0,m,s1,s2; m=2*n+1; s1=n; s2=m; double temp=s1/s2,sum=0.0; while(temp>=eps){ sum+=temp; n+=1; m+=2; s1*=n; s2*=m; temp=s1/s2; } return (sum+1)*2; 1.3​ 求阶乘 #include float fun(int m, int n) { } int main(void) /* 主函数 */ { printf("P=%f\n", fun (12,8)); //NONO(); return 1; } int NONO (void) {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *fp, *wf ; int i, m, n ; float s; fp = fopen("c:\\test\\in.dat","r") ; wf = fopen("c:\\test\\out.dat","w") ; for(i = 0 ; i < 10 ; i++) { fscanf(fp, "%d,%d", &m, &n) ; s = fun(m, n) ; fprintf(wf, "%f\n", s) ; } fclose(fp) ; fclose(wf) ; return 1; } 答案: float result; int x,y,z,o=1,p=1,q=1; for(x=1;x<=m;x++){ o=o*x; } for(y=1;y<=n;y++){ p=p*y; } for(z=1;z<=m-n;z++){ q=q*z; } result=o/(p*q); return result; 尝试建立一个专门求阶乘的函数。 1.4​ 求公式 #include double fun( int n ) { } int main(void) /* 主函数 */ { printf("%f\n", fun(10)); //NONO(); return 1; } int NONO (void) {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *fp, *wf ; int i, n ; double s ; fp = fopen("c:\\test\\in.dat","r") ; wf = fopen("c:\\test\\out.dat","w") ; for(i = 0 ; i < 10 ; i++) { fscanf(fp, "%d", &n) ; s = fun(n) ; fprintf(wf, "%f\n", s) ; } fclose(fp) ; fclose(wf) ; return 1; } 答案: int i; double m,sum=0.0; for(i=1;i<=n;i++){ m=i*(i+1); sum+=1/m; } return sum; 1.5​ 整数分解 函数fun的功能是:将a、b中的两个两位正整数合并形成一个新的整数放在c中。合并的方式是:将a中的十位和个位数依次放在变量c的十位和千位上,b中的十位和个位数依次放在变量c的个位和百位上。 例如,当a=45,b=12。调用该函数后,c=5241。 注意: 部分源程序存在文件PROG1.C中。数据文件IN.DAT中的数据不得修改。 请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 #include void NONO(void); long fun(int a, int b) { } int main(void) { int a,b; long c; printf("Input a, b:"); scanf("%d%d", &a, &b); c=fun(a, b); printf("The result is: %ld\n", c); NONO(); return 0; } void NONO (void) {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *rf, *wf ; int i, a,b ; long c ; rf = fopen("./44/in.dat","r") ; wf = fopen("./44/out.dat","w") ; for(i = 0 ; i < 10 ; i++) { fscanf(rf, "%d,%d", &a, &b) ; fun(a, b, &c) ; fprintf(wf, "a=%d,b=%d,c=%ld\n", a, b, c) ; } fclose(rf) ; fclose(wf) ; } 答案: long i1=(a%10)*1000; printf("aa=%d,bb=%d",b%10,b/10); long i2=(b%10)*100; long i3=a/10*10; long i4=b/10; //return a%10*1000+b%10*100+a/10*10+b/10; return i1+i2+i3+i4; 1.6​ 数学公式 #include /************found************/ fun(int m){ double y = 0, d; int i ; /************found************/ for( i = 100;i < m;i += 100 ) { d = (double)i * (double)i ; y += 1.0 / d ; } return (y); } main( ) { int n = 2000 ; printf( "\nThe result is %lf\n", fun ( n ) ) ; } 答案: double fun ( int m ) { for( i = 100;i <=m;i += 100 ) { 1.7​ 数学公式 修正: 当k=1时,第一项为4/1*3 #include #include float fun(int k){ } main ( ) { printf("%f\n", fun (10)); } 答案1: int n; float s, w, p, q; n = 1; s = 1.0; while ( n <= k ) { w = 2.0 * n; p = w - 1.0; q = w + 1.0; s = s * w *w/p/q; n++; } return s; 改错: int i; float sum=1.0; for(i=2;i<=k;i++){ sum*=(2*i * 2*i)/((2*i+1)*(2*i-1)); //注意数据类型 } return sum; 1.8​ 最小公倍数 给定程序MODI1.C中函数fun的功能是:求三个数的最小公倍数。 例如,给主函数中的变量x1、x2、x3分别输入15 11 2, 则输出结果应当是:330。请改正程序中的错误,使它能得出正确结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 #include /************found************/ fun(int x, y, z ) { int j,t ,n ,m; j = 1 ; t=j%x; m=j%y ; n=j%z; while(t!=0||m!=0||n!=0) { j = j+1; t=j%x; m=j%y; n=j%z; } /************found************/ return i; } main( ) { int x1,x2,x3,j ; printf("Input x1 x2 x3: "); scanf("%d%d%d",&x1,&x2,&x3); printf("x1=%d, x2=%d, x3=%d \n",x1,x2,x3); j=fun(x1,x2,x3); printf("The minimal common multiple is : %d\n",j); } 答案: int fun(int x,int y,int z){ return j; 编程: #include //求三个数的最大数 int max(int x,int y,int z){ } //求三个数最小公倍数 int fun(int x, int y, int z ) { } main( ) { int x1,x2,x3,j ; printf("Input x1 x2 x3: "); scanf("%d%d%d",&x1,&x2,&x3); printf("x1=%d, x2=%d, x3=%d \n",x1,x2,x3); j=fun(x1,x2,x3); printf("The minimal common multiple is : %d\n",j); } 答案: int temp; if (x>y) temp=x; else temp=y; if(temp >z) return temp; else return z; int temp=max(x,y,z); int result=temp; while(1){ if(result%x==0 && result%y==0 && result%z==0) return result; result=result+temp; } 1.9​ n!改错 给定程序MODI1.C中函数 fun 的功能是:计算n!。 例如,给n输入5,则输出120.000000。 请改正程序中的错误,使程序能输出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! #include double fun ( int n ) { double result = 1.0 ; /************found************/ if n = = 0 return 1.0 ; while( n >1 && n < 170 ) /************found************/ result *= n-- return result ; } main ( ) { int n ; printf("Input N:") ; scanf("%d", &n) ; printf("\n\n%d! =%lf\n\n", n, fun(n)) ; } 答案: if(n==0) return 1.0; result*=n--; 1.10​ 数学公式 #include double fun(int n){ } main( ) { int n = 5; printf( "\nThe value of function is: %lf\n", fun ( n ) ); } 答案: int a, b, c, k; double s; s = 0.0; a = 2; b = 1; for ( k = 1; k <= n; k++ ) { s=s+(double)a/b; c = a; a = a + b; b = c; } return s; 1.11​ 数列 已知一个数列从第0项开始的前三项分别为0,0,1,以后的各项都是其相邻的前三项之和。给定程序MODI1.C中函数fun的功能是:计算并输出该数列前n项的平方根之和。n的值通过形参传入。 例如,当n=10时,程序的输出结果应为:23.197745。 请改正程序中的错误,使程序能输出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! #include #include double fun(int n){ } main ( ) { int n; printf("Input N="); scanf("%d", &n); printf("%f\n", fun(n) ); } 答案: double sum, s0, s1, s2, s; int k; sum = 1.0; if (n <= 2) sum = 0.0; s0 = 0.0; s1 = 0.0; s2 = 1.0; for (k = 4; k <= n; k++) { s = s0 + s1 + s2; sum += sqrt(s); s0 = s1; s1 = s2; s2 = s; } return sum; 1.12​ 数学公式 #include /************found************/ int fun ( int n ) { float A=1; int i; /************found************/ for (i=2; i double fun ( int m ) { double y = 1.0 ; int i ; /**************found**************/ for(i = 2 ; i < m ; i++) /**************found**************/ y -= 1 /(i * i) ; return( y ) ; } main( ) { int n = 5 ; printf( "\nThe result is %lf\n", fun ( n ) ) ; } 答案: for(i=2;i<=m;i++) y -=1.0/(i*i); 1.14​ 数学公式 #include #include double fun(double eps) { double s,t; int n=1; s=0.0; /************found************/ t=0; while( t>eps) { s+=t; t=t * n/(2*n+1); n++; } /************found************/ return (s); } main() { double x; printf("\nPlease enter a precision: "); scanf("%lf",&x); printf("\neps=%lf, Pi=%lf\n\n",x,fun(x)); } 答案: t=1.0; return(s*2); 1.15​ 素数 给定程序MODI1.C中函数fun的功能是:找出一个大于形参m且紧随m的素数,并作为函数值返回。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! #include int fun(int m) { int i, k ; for (i = m + 1 ; ; i++) { for (k = 2 ; k < i ; k++) /**************found**************/ if (i % k != 0) break ; /**************found**************/ if (k < i) return(i); } } void main() { int n ; n = fun(20) ; printf("n=%d\n", n) ; } 答案: if(i%k==0)break; if(k==i)return (i); 1.16​ 整数处理 给定程序MODI1.C中函数fun的功能是:从低位开始取出长整型变量s中偶数位上的数,依次构成一个新数放在t中。高位仍在高位,低位仍在低位。 例如,当s中的数为:7654321时,t中的数为:642。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! #include /************found************/ //void fun (long s, long t) { void fun(long s,long *t){ long sl=10; s /= 10; *t = s % 10; /************found************/ //while ( s < 0) { while(s>0){ s = s/100; *t = s%10*sl + *t; sl = sl * 10; } } main(){ long s, t; printf("\nPlease enter s:"); scanf("%ld", &s); fun(s, &t); printf("The result is: %ld\n", t); } 1.17​ 自然数 给定程序MODI1.C中函数fun的功能是:计算小于形参k的最大的10个能被13或17整除的自然数之和。k的值由主函数传入,若k的值为500,则函数值为4622。 请改正程序中的错误,使程序能输出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! #include int fun( int k ) { int m=0, mc=0, j ; while ((k >= 2) && (mc < 10)) { /************found************/ if ((k%13 = 0) || (k%17 = 0)) { m = m+ k; mc++; } k--; } return m; /************found************/ _____ } main ( ) { printf("%d\n",fun (500)); } if(k%13 == 0 || k%17 == 0){ //____
本文档为【二级c语言上机试题总结(数学计算)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_607596
暂无简介~
格式:doc
大小:464KB
软件:Word
页数:17
分类:计算机考试
上传时间:2011-02-23
浏览量:17