toj 4609 Internal Rate of Return

来源:互联网 发布:配眼镜需要的数据 编辑:程序博客网 时间:2024/06/06 11:05

toj 4609 Internal Rate of Return


时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte
总提交: 17 测试通过:11

描述

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:

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.

输入

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.

输出

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).

样例输入

1
-1 2
2
-8 6 9
0

样例输出

1.00
0.50

#include <iostream>#include <cmath>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;#define eps 1e-5int main(){    int T, s[10000];    while(scanf("%d", &T) && T)    {        memset(s, 0, sizeof(s));        for(int i = 0; i <= T; i++)        {            scanf("%d", &s[i]);        }        //printf("%d\n", s[0]);        double Q = 0;        double left = -1;        double right = 10000;        int flag = 1;        double mid = 0;        while(1)        {            mid = (left + right) / 2;            //cout << mid << endl;            Q = s[0];            for(int i = 1; i <= T; i++)            {                if((1+mid) == 0)                {                    flag = 0;                    break;                }                Q += s[i]/pow(1+mid, i*1.0);                //cout << Q << endl;            }            if(flag == 0)            {                flag == 1;                continue;            }            if(Q > eps)                left = mid;            else if(Q < -eps)                right = mid;            if(Q <= eps && Q >= -eps)                    break;        }        printf("%.2lf\n", mid);    }    return 0;}
0 0
原创粉丝点击