Uva 11881 - Internal Rate of Return
你为何这么叼!
Time limit: 1.000 seconds
In finance, Internal Rate of Return (IRR) is the discount rate of an investment when NPV equals zero. Formally, given T, CF0, CF1, ..., CFT, then IRR is the solution to the following equation:
+
+ K +
= 0
Your task is to find all valid IRRs. In this problem, the initial cash-flow CF0 < 0, while other cash-flows are all positive (CFi > 0 for all i = 1, 2,...).
Important: IRR can be negative, but it must be satisfied that IRR > - 1.
Input
There will be at most 25 test cases. Each test case contains two lines. The first line contains a single integer T ( 1
T
10), the number of positive cash-flows. The second line contains T + 1 integers: CF0, CF1, CF2, ..., CFT, where CF0 < 0, 0 < CFi < 10000 ( i = 1, 2,..., T). The input terminates by T = 0.
Output
For each test case, print a single line, containing the value of IRR, rounded to two decimal points. If no IRR exists, print ``No" (without quotes); if there are multiple IRRs, print ``Too many"(without quotes).
Sample Input
1 -1 2 2 -8 6 9 0
Sample Output
1.00 0.50
Problemsetter: Rujia Liu, Special Thanks: Yiming Li, Shamim Hafiz & Sohel Hafiz
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <stdio.h> 3 #include <string.h> 4 #include <iostream> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <set> 9 #include <map> 10 #include <string> 11 #include <math.h> 12 #include <stdlib.h> 13 #include <time.h> 14 using namespace std; 15 #define maxn 101 16 #define INF 0x7fffffff 17 int n; 18 int a[maxn]; 19 double fun(double m){ 20 double s=0.0,x=1.0; 21 for(int i=1;i<=n;i++){ 22 x*=(m+1); 23 s+=a[i]/x; 24 } 25 return s; 26 } 27 double find(){ 28 double l=-1.0,r=INF*1.0,m; 29 for(int i=0;i<=100;i++){ 30 m=l+(r-l)/2; 31 if(fun(m)>=-a[0])l=m; 32 else r=m; 33 } 34 return m; 35 } 36 int main(){ 37 while(scanf("%d",&n)&&n){ 38 int k=0; 39 for(int i=0;i<=n;i++){scanf("%d",&a[i]);if(a[i]>0)k++;} 40 if(k==n+1){printf("No\n");continue;} 41 printf("%.2lf\n",find()); 42 } 43 return 0; 44 }
浙公网安备 33010602011771号