HUNNU10934 内部收益率

  原题链接:http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=10934&courseid=0

  double,二分。

View Code
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 #include <stdlib.h>
 5 #define INF 1000000000.0
 6 const double eps = 1e-8;
 7 double a[11];
 8 int n;
 9 
10 double cal(double k)
11 {
12     double ans = 0.0;
13     for(int i = 1; i <= n; i ++)
14     {
15         ans += a[i] * pow(k, i + 0.0);
16     }
17     return ans;
18 }
19 
20 double bs()
21 {
22     double l = 0.0, r = INF;
23     a[0] = a[0] * (-1);
24     while(r - l > eps)
25     {
26         double mid = (r + l) * 0.5;
27         double tmp = cal(mid);
28         if(tmp >= a[0])
29             r = mid;
30         else
31             l = mid;
32     }
33     return l;
34 }
35 
36 int main()
37 {
38     int i;
39     while(scanf("%d", &n), n > eps)
40     {
41         scanf("%lf", &a[0]);
42         for(i = 1; i <= n; i ++)
43             scanf("%lf", &a[i]);
44         
45         double ans = bs();
46         if(ans > INF - 1.0)
47         {
48             printf("No\n");
49             continue;
50         }
51         ans = 1.0 / ans;
52         ans -= 1.0;
53         printf("%.2f\n", ans);
54     }
55     return 0;
56 }

你可能感兴趣的:(HUNNU10934 内部收益率)