toj 4609 Internal Rate of Return

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-5

int 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值