c语言计算机二级试题(计算机等级考试二级C语言程序设计专项训练题——多项式求值)
在计算机等级考试二级C语言程序设计试题中 ,多项式求值是一个重要的考点 ,有关多项式求值的试题在历年考试试卷的程序填空题和程序设计题中经常出现 。
一.示例讲解1.求给定多项式的前n项之和 。
这类题目中 ,给定了需要计算的多项式的项数n ,并且各项通常可以用数学式表示出来。因此 ,程序通常写成如下循环:
for (i=1; i<=n; i++)
{
// 按各项的数学式求出当前第i项
// 将第i项累加到多项式和值上
}
例1 编写函数fun ,它的功能是:计算序列 1 + 1/2 + 1/3 + ...的前N项之和 。
例如 ,若n=10 ,函数值为:2.928968 。
#include <stdio.h>
double fun(int n)
{
}
int main()
{
printf("%f\n", fun(10));
return 0;
}
解析:直接利用for循环完成累加求和 。
编写的fun函数如下:
double fun(int n)
{
double s=0;
int i;
for (i=1;i<=n;i++)
s=s+1.0/i; // 注意:不要写成 1/i
return s;
}
例2 编写函数fun ,它的功能是:计算简单交错序列
1 - 1/4 + 1/7 - 1/10 + ... 的前N项之和 。
例如,若n=10 ,函数值为:0.818743 。
#include <stdio.h>
double fun(int n)
{
}
int main()
{
printf("%f\n", fun(10));
return 0;
}
对于这种交错序列的多项式求值 ,可以引入一个变量t用于表示正负号切换,初始时 ,t=1 ,每累加一项后,t=-t ,这样t的值在1 、-1 、1 、-1 、…序列中变换 ,正好和交错序列加1项 ,减1项相符合 。
编写的fun函数如下:
double fun(int n)
{
double s=0;
int i,t=1;
for (i=1;i<=n;i++)
{
s=s+t*1.0/(3*i-2);
t=-t;
}
return s;
}
例3 编写函数fun ,它的功能是:计算
例如 ,若x=2.5 ,n=12 ,函数值为:12.182340 。
#include <stdio.h>
double fun(double x, int n)
{
}
int main()
{
double x, y;
x=2.5;
y = fun(x, 12);
printf("\nThe result is :\n");
printf("x=%-12.6f y=%-12.6f\n", x, y);
return 0;
}
解析:在这个题目中 ,直接按每项的数学表示式求出该项的值不是一个可取的办法 。实际上若求得了第i项的值为t ,则第i+1项的值为 t*x/(i+1) 。因此,可设t的初始值为 1 ,之后用循环
for (i=1; i<=n; i++)
t *= x/i;
可依次求得第1项至第n项的值。
例如 ,i=1时,t*=x/1; t=1*x/1=x;
i=2时 ,t*=x/2; t=x*(x/2)=x2/2! ;
i=3时 ,t*=x/3; t= x2/2!*(x/3)= x3/3!;
……
编写的fun函数如下:
double fun(double x, int n)
{
double f, t;
int i;
f = 1.0;
t = 1.0;
for (i=1; i<n; i++)
{
t *= x/i;
f += t;
}
return f;
}
例4 编写函数fun,它的功能是:计算
例如 ,若x=2.5 ,n=15 ,函数值为:1.917915 。
#include <stdio.h>
#include <math.h>
double fun(double x, int n)
{
}
int main()
{
double x, y;
x=2.5;
y = fun(x, 15);
printf("\nThe result is :\n");
printf("x=%-12.6f y=%-12.6f\n", x, y);
return 0;
}
解析:如同例3 ,直接由第i项的值t求得第i+1项 。t的初始值设为-1.0。
编写的fun函数如下:
double fun(double x, int n)
{
double f, t;
int i;
f = 1.0;
t = -1;
for (i=1; i<=n; i++)
{
t *= (-1.0)*x/i;
f += t;
}
return f;
}
2.求给定精度的多项式的值 。
在这类题目中 ,通常组成多项式的各项是一个收敛序列 ,要求计算多项式的值 ,直到最后一项的绝对值小于给定的精度值eps 。程序写成
// 置多项式的初始值 ,并按要求置初始的当前项item
while (fabs(item)>=eps)
{
// 累加当前项item
// 按多项式各项表示式求出下一项 ,作为新的当前项item
}
也可以写成
// 置多项式的初始值,并按要求置初始的当前项item
do
{
// 累加当前项item
// 按多项式各项表示式求出下一项 ,作为新的当前项item
} while (fabs(item)>=eps);
例5 编写函数fun ,它的功能是:计算序列部分和
1 - 1/4 + 1/7 - 1/10 + ...
直到最后一项的绝对值不大于给定精度eps。
#include <stdio.h>
#include <math.h>
double fun(double eps)
{
}
int main()
{
double e;
scanf("%lf", &e);
printf("sum = %.6f\n", fun(e));
return 0;
}
编写的fun函数如下:
double fun(double eps)
{
int t=1, i=1;
double sum=0,p=1.0;
if (eps>=1)
return p;
else
{
while (fabs(p)>eps)
{
p=t*1.0/i;
sum=sum+p;
i+=3;
t=-t;
}
return sum;
}
}
例6 编写函数fun,它的功能是:计算
直到|xn/n!|<10-6 。
例如 ,若x=2.5 ,函数值为:12.182494 。
#include <stdio.h>
#include <math.h>
double fun(double x)
{
}
int main()
{
double x, y;
x=2.5;
y = fun(x);
printf("\nThe result is :\n");
printf("x=%-12.6f y=%-12.6f \n", x, y);
return 0;
}
解析:如同例3,直接由第i项的值t求得第i+1项 。
编写的fun函数如下:
double fun(double x)
{
double f, t;
int n;
f = 1.0+x;
t = x;
n = 1;
do {
n++;
t *= x/n;
f += t;
}while (fabs(t) >= 1e-6);
return f;
}
例7 编写函数fun ,它的功能是:计算
直到|xn/n!|<10-6 。
例如 ,若x=2.5 ,函数值为:1.917915 。
#include <stdio.h>
#include <math.h>
double fun(double x)
{
}
int main()
{
double x, y;
x = 2.5;
y = fun(x);
printf("\nThe result is :\n");
printf("x=%-12.6f y=%-12.6f\n", x, y);
return 0;
}
解析:结合例4和例6的思路 ,将例6中的t *= x/n;简单修改为 t *= (-1.0)*x/n; 即可 。
编写的fun函数如下:
double fun(double x)
{
double f, t;
int n;
f = 1.0 + x;
t = x;
n = 1;
do {
n++;
t *= (-1.0)*x/n;
f += t;
} while (fabs(t)>= 1e-6);
return f;
}
例8 编写函数fun ,它的功能是:用下列公式求cos(x)的近似值 ,精确到最后一项的绝对值小于e:
例如 ,若e=0.01 ,x=-3.14 ,则函数值为-0.999899 。
#include <stdio.h>
#include <math.h>
double fun(double e,double x)
{
}
int main()
{
double e, x;
scanf("%lf %lf", &e, &x);
printf("cos(%.2f) = %.6f\n", x, fun(e, x));
return 0;
}
编写的fun函数如下:
double fun(double e,double x)
{
double cos=1.0,t=1.0;
int n=0;
while (fabs(t)>=e)
{
t=(-1)*t*x*x/((n+1)*(n+2));
cos+=t;
n+=2;
}
return cos;
}
3.给定的多项式序列各项用文字描述 。
在这类题目中,给定的多项式序列的各项无法用一个数学表达式精确表示 ,通常用文字进行描述 。例如 ,求1000以内所有的质数的和。
例9 编写函数fun,它的功能是:计算n(包括n)以内能被5或9 整除的所有自然数的倒数之和 。
例如 ,在主函数中输入n为20 ,则输出为:s=0.583333 。
#include <stdio.h>
double fun(int n)
{
}
int main()
{
int n;
double s;
printf("\nInput n: ");
scanf("%d",&n);
s=fun(n);
printf("\ns=%f\n",s);
return 0;
}
解析:程序写成一重循环for (i=1;i<=n;i++),循环体中 ,对每个整数i进行判断 ,如果i能被5或9 整除 ,则累加i的值。
编写的fun函数如下:
double fun(int n)
{
double s=0.0;
int i;
for (i=1;i<=n;i++)
if (i%5==0 || i%9==0)
s+=1.0/i;
return s;
}
例10 编写函数fun ,它的功能是:求s= a +aa+aaa +… +aa..aa(此处aa..aa表示n个a ,a和n的值在1至9之间) 。
例如 ,a=3,n=5 ,则以上表达式为:s=3+33+333+3333+33333 ,其值是:37035 。
#include <stdio.h>
int fun (int a, int n)
{
}
int main( )
{
int a, n ;
printf( "\nPlease enter a and n:") ;
scanf( "%d%d", &a, &n ) ;
printf( "The value of function is: %d\n", fun(a,n));
return 0;
}
解析:aa⋯a(n个a)可以看成由n-1个a乘以10加上a得到。初始时设t=0 ,每次循环执行t=t*10+a 。显然,第1次循环(i=1)时 ,t=0*10+a=a;第2次循环(i=2)时 ,t=a*10+a=aa;…;第n次循环(i=n)时,t= aa⋯a(n-1个a)*10+a= aa⋯a(n个a) 。
编写的fun函数如下:
int fun (int a, int n)
{
int i,s=0, t=0 ;
for (i = 1 ; i<=n ; i++)
{
t = t*10+a;
s = s + t ;
}
return(s) ;
}
二.程序设计练习题1. 编写函数fun ,它的功能是:计算下列级数和
例如 ,当n=10时,函数值为 0.909091 。
#include <stdio.h>
double fun(int n)
{
}
int main()
{
printf("%f\n", fun(10));
return 0;
}
2. 编写函数fun ,它的功能是:计算
例如 ,在主函数中从键盘输入8后 ,输出为:s=0.662872 。
#include <stdio.h>
double fun(int n)
{
}
int main()
{
int n;
double s;
printf("\nInput n: ");
scanf("%d",&n);
s=fun(n);
printf("\ns=%f\n",s);
return 0;
}
参考程序
3. 编写函数fun ,它的功能是:求出以下分数序列的前n项之和 。
2/1 ,3/2 ,5/3 ,8/5 ,13/8 ,21/13,……
例如 ,若n=5 ,则应输出:8.391667 。
#include <stdio.h>
double fun(int n)
{
}
int main( )
{
int n = 5 ;
printf("\nThe value of function is: %f\n", fun(n));
return 0;
}
参考程序
4. 编写函数fun,它的功能是:计算并输出下列级数的前N项之和SN ,直到SN+1大于q为止 ,q的值通过形参传入 。
SN= 2/1+3/2+4/3+……+(N+1)/N
例如,若q的值为50.0 ,则函数值为49.394948 。
#include <stdio.h>
double fun(double q)
{
}
int main()
{
printf("%f\n",fun(50.0));
return 0;
}
参考程序
5.编写函数fun ,它的功能是:计算
例如 ,若m=20 ,函数值为:6.506583 。
#include <stdio.h>
#include <math.h>
double fun(int m)
{
}
int main()
{
printf("%f\n", fun(20));
return 0;
}
参考程序
6. 编写函数fun ,其功能是:根据以下公式计算S ,计算结果作为函数值返回 ,n通过形参传入。
例如 ,若n=11 ,函数返回值为1.833333 。
#include <stdio.h>
float fun(int n)
{
}
int main()
{
int n;
float s;
printf("\nPlease enter N:");
scanf("%d", &n);
s = fun(n);
printf("the result is: %f\n", s);
return 0;
}
参考程序
7. 编写函数fun,它的功能是:计算
例如 ,n=20时 ,S=534.188884 。
#include <stdio.h>
#include <math.h>
double fun(int n)
{
}
int main()
{
int n;
double s;
printf("\n\nInput n: ");
scanf("%d",&n);
s=fun(n);
printf("\ns=%f\n",s);
return 0;
}
参考程序
8. 编写函数fun,其功能是:计算下列多项式的值。
例如 ,若n=50 ,函数值为:1.718282 。
#include <stdio.h>
double fun(int n)
{
}
int main()
{
int n;
double s;
printf("Input n: ");
scanf("%d",&n);
s=fun(n);
printf("\ns=%f\n",s);
return 0;
}
参考程序
9. 编写函数fun,它的功能是:用下面的公式求π的近似值 ,直到最后一项的绝对值小于指定的数num为止 。
π/4=1-1/3+1/5-1/7+…
例如 ,程序运行后 ,输入0.0001 ,则程序输出3.1414。
#include <stdio.h>
float fun(float num)
{
}
int main( )
{
float n1, n2 ;
printf("Enter a float number: ") ;
scanf("%f", &n1) ;
n2 = fun(n1) ;
printf("%6.4f\n", n2) ;
return 0;
}
参考程序
10.编写函数fun ,它的功能是:根据下列公式求π值 。
例如 ,给定精度eps为0.0005时 ,应当输出pi=3.140578 。
#include <stdio.h>
double fun(double eps)
{
}
int main()
{
double x;
printf("\nPlease enter a precision: ");
scanf("%lf",&x);
printf("\neps=%f, Pi=%f\n",x,fun(x));
return 0;
}
参考程序
11.编写函数fun ,它的功能是:计算并输出给定10个数的方差 。
例如 ,给定的10个数为95.0 、89.0 、76.0 、65.0 、88.0 、72.0、85.0 、81.0 、90.0、56.0,输出为s=11.730729 。
#include <stdio.h>
#include <math.h>
double fun(double x[10])
{
}
int main()
{
double s, x[10]={95.0,89.0,76.0,65.0,88.0,72.0,85.0,81.0,90.0,56.0};
int i;
printf("\nThe original data is :\n");
for(i=0;i<10;i++) printf("%6.1f",x[i]);
printf("\n\n");
s=fun(x);
printf("s=%f\n",s);
return 0;
}
参考程序
12.编写函数fun ,它的功能是:计算下列多项式的值
例如 ,当n=10时,输出结果为:9.612558 。
#include <stdio.h>
double fun(int n)
{
}
int main()
{
int n=-1;
while(n<0)
{
printf("Please input(n>0): ");
scanf("%d",&n);
}
printf("\nThe result is: %f\n",fun(n));
return 0;
}
参考程序
13.编写函数fun ,它的功能是:计算下列多项式的值
例如 ,当n=10时,输出结果为:-0.204491 。
#include <stdio.h>
double fun(int n)
{
}
int main()
{
int n;
scanf("%d",&n);
printf("\nThe result is: %f\n",fun(n));
return 0;
}
参考程序
14.编写函数fun ,它的功能是:计算
例如 ,若形参e的值为1e-3 ,函数的返回值为2.985678 。
#include <stdio.h>
double fun(double e)
{
}
int main()
{
double e=1e-3;
printf("\nThe result is: %f\n",fun(e));
}
参考程序
15.编写函数fun ,它的功能是:计算
#include <stdio.h>
double fun(double e)
{
}
int main()
{
double e=1e-3;
printf("\nThe result is: %f\n",fun(e));
return 0;
}
参考程序
16.编写函数fun ,它的功能是:计算当x<0.97时下列多项式的值 ,直到|S(n)-S(n-1)|<0.00001为止 。
例如 ,在主函数中从键盘给x输入0.21后 ,输出为:S=1.100000 。
#include <stdio.h>
#include <math.h>
double fun(double x)
{
}
int main()
{
double x,s;
printf("Input x: ");
scanf("%lf",&x);
s=fun(x);
printf("s=%f\n",s);
return 0;
}
参考程序
17.编写函数fun ,它的功能是:求s=aa..aa-…aaa-aa-a(此处aa..aa表示n个a,a和n的值在1至9之间)。
例如 ,a=3,n=6 ,则以上表达式为:s=333333-33333-3333-333-33-3,其值是:296298 。
#include <stdio.h>
long fun (int a, int n)
{
}
int main( )
{
int a, n ;
printf( "\nPlease enter a and n:") ;
scanf( "%d%d", &a, &n ) ;
printf( "The value of function is: %ld\n", fun(a,n));
return 0;
}
参考程序
18.编写函数fun ,它的功能是:求n以内(不包括n)同时能被3和7整除的所有自然数之和的平方根s ,并作为函数值返回 。
例如,n=1000时 ,函数值应为:s=153.909064。
#include <stdio.h>
#include <math.h>
double fun(int n)
{
}
int main()
{
printf("s=%f\n",fun(1000));
return 0;
}
参考程序
19.编写函数fun ,它的功能是:计算给定整数n的所有因子(不包括1和n自身)之和 。规定n的值不大于1000 。
例如 ,若n输入的值为856,则输出为:sum=763。
#include <stdio.h>
int fun(int n)
{
}
int main()
{
int n,sum;
printf("Input n: ");
scanf("%d",&n);
sum=fun(n);
printf("sum=%d\n",sum);
return 0;
}
参考程序
20.已知一个数列从第0项开始的前三项分别为0,0,1 ,以后的各项都是其相邻的前三项之和 。给定程序中 ,函数fun的功能是:计算并输出该数列前n项的平方根之和sum 。
例如 ,当n=10时 ,程序的输出结果应为:23.197745 。
#include <stdio.h>
#include <math.h>
double fun(int n)
{
}
int main()
{
int n;
printf("Input N=");
scanf("%d", &n);
printf("%f\n", fun(n));
return 0;
}
参考程序
创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!