Function Evaluation
Author: Leisureeen
Time Limit: 100ms
Memory Limit: 65535KB
Code Size Limit: 16 KB
64-bit integer IO format: %lld
Here given a function, you are supposed to calculate the function value.
Input Specification:
Each input file contains one test case. For each test case, the first line gives only one positive integer x (1 <= x <= 5).
Output Specification:
For each test case you should output the function value in one line. Notice that there must be NO extra space at the end of the line. Please be accurate to x decimal place.
Sample Input:
1
Sample Output:
0.9
本人针对此题给出2种思路,首先我们可以将sinx展开,逐项积分,得到结果如下图。
然后编程实现即可,注意这里不要加到无穷,精确到题目要求后几位就行了,代码如下。
1 #include<stdio.h> 2 3 int main(void) 4 { 5 int x=0,n=0; 6 for(x=1;x<=10;x++) 7 { 8 double m=0.0,s=0.0; 9 for(n=1,m=x;m/n>1e-8;n+=2) 10 { 11 if(n-1&3) 12 s-=m/n; 13 else 14 s+=m/n; 15 m*=x*x; 16 m/=n+1; 17 m/=n+2; 18 } 19 printf("%.8lf\n",s); 20 } 21 return 0; 22 }
运行结果如下。
0.94608307 1.60541298 1.84865253 1.75820313 1.54993124 1.42468755 1.45459661 1.57418682 1.66504008 1.65834760
然后本题还可以利用定积分的定义(被积函数在x=0处无定义,但极限值为1)来做,无限(实际上是分得很小)分割对小矩形面积求和,代码如下。
1 #include<stdio.h> 2 #include<math.h> 3 #define M 8000000.0 4 5 int main(void) 6 { 7 int x=0; 8 for(x=1;x<=10;x++) 9 { 10 double m=0.0,s=1.0; 11 for(m=1/M;m<(double)x;m+=1/M) 12 s+=sin(m)/m; 13 printf("%.8lf\n",s/M); 14 } 15 return 0; 16 }
运行结果如下。
0.94608308 1.60541307 1.84865259 1.75820321 1.54993130 1.42468761 1.45459668 1.57418689 1.66504014 1.65834765
最后针对本题,可以写出解题代码如下。
1 #include<stdio.h> 2 3 int main(void) 4 { 5 int x=0; 6 char *key[]={"0.9","1.61","1.849","1.7582","1.54993"}; 7 scanf("%d",&x); 8 printf("%s",key[x-1]); 9 return 0; 10 }
posted on 2019-11-28 22:54 Leisureeen 阅读(450) 评论(0) 编辑 收藏 举报