所属年份:2010.9;2012.3
【程序填空题】
下列给定程序中,函数fun的功能是:计算如下公式前n项的和并作为函数值返回。
例如,当形参n的值为10时,函数返回值为9.612558。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h> double fun(int n) { int i; double s, t; /**********found**********/ s=__1__; /**********found**********/ for(i=1; i<=__2__; i++) { t=2.0*i; /**********found**********/ s=s+(2.0*i-1)*(2.0*i+1)/__3__; } return s; } main() { int n=-1; while(n<0) { printf("Please input(n>0): "); scanf("%d",&n); } printf("\nThe result is: %f\n",fun(n)); } |
【程序修改题】
下列给定程序中函数fun的功能是:统计substr所指的子符串在str所指的字符串中出现的次数。
例如,若字符串为aaas 1kaaas,子字符串为as,则应输出2。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h> int fun (char *str,char *substr) { int i,j,k,num=0; /************found************/ for(i = 0, str[i], i++) for(j=i,k=0;substr[k]==str[j];k++,j++) /************found************/ If(substr[k+1]=='\0') { num++; break; } return num; } main() { char str[80],substr[80]; printf("Input a string:") ; gets(str); printf("Input a substring:") ; gets(substr); printf("%d\n",fun(str,substr)); } |
【程序设计题】
编写函数fun,其功能是:根据以下公式求π的值(要求精度0.0005,即某项小于0.0005时停止迭代)。
程序运行后,若输入精度0.0005,则程序应输出为3.14…。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <stdio.h> #include <math.h> double fun ( double eps) { } main( ) { double x; printf("Input eps:") ; scanf("%lf",&x); printf("\neps = %lf, PI=%lf\n", x, fun(x)); } |