1076 problem: internal rate of return

1076 problem: internal rate of return

Time limit: 1Sec Memory Limit: 128MB submit: 418 Resolution: 169

Title Description

In finance, we sometimes use internal rate of return IRR to evaluate the financial benefits of the investment project, which is equal to the net present value of investment makes NPV equal to zero discount rate. In other words, given project periods T , the initial cash flow CF2 0 and projects of cash flow CF2 . 1,  CF2 2, ..., CFT , the IRR is solving the following equation:

 

 

 

For simplicity, assume that this problem: in addition to a sum of inputs (i.e., the initial start of the project cash flow CF2 0 <0), the rest periods can money (i.e., for all I = 1,2, ..., T , CF2 i> 0). By definition, the IRR can be negative, but not greater than -1.

Entry

 Input file contains up to 25 test cases, each of the data over two lines, the first line contains a positive integer T (. 1 <= T <= 10), indicates the item number of periods. The second line contains T + 1'd integers: CF2 0,  CF2 . 1,  CF2 2, ...,  CFT , wherein CF2 0 <0, 0 <  CFi  <10000 ( I = 1,2, ..., T ). T = 0 represents the input end, your program should not deal with this line.

Export

 For each test, output a line only, i.e., projects the IRR , rounded to two decimal places. If the IRR is not present, the output of "No", if there are a plurality of different IRR condition is satisfied, the output "Too many" (both without the quotes)

Sample input
1
-1 2
2
-8 6 9
0
Sample Output
1.00
0.50

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main{
public static void main(String args[]){
  Scanner in = new Scanner(System.in);
  DecimalFormat df = new DecimalFormat("##0.00");
  String str="";
  while(true){
    //期数
    int T = in.nextInt();
    //保存每期的现金流
    int a[] = new int[T+1];
    if(T==0)
      break;
    //输入每期的现金流
    else{
      for(int i=0;i<=T;i++){
        a[i] = in.nextInt();
  }
    double L = -1.0;
    double R = 9999.0;
    for(int i=0;i<100;i++){
      double IRR = (L+R)/2.0;
      double sum=0;
      double fenmu=1;
      for(int j=0;j<=T;j++){
        sum+=a[j]/fenmu;
        fenmu=fenmu*(1+IRR);
    }
    if(sum>0)
      L=IRR;
    else
      R=IRR;
  }
    str+=df.format(L)+"\n";
}
}
  System.out.println(str);
}
}

Guess you like

Origin www.cnblogs.com/xuesujun/p/11432086.html