1017. Rate of Return (用二分法求近似解)

来源:互联网 发布:淘宝手机助手官方版 编辑:程序博客网 时间:2024/05/21 08:56
/*1017. Rate of Return  用二分法求近似解。 */#include <iostream>#include <stdlib.h>#include <stdio.h>#include <math.h>using namespace std;int main(){     freopen("in.txt", "r", stdin);    freopen("out.txt", "w", stdout);    int n,caseNum = 1, terminalMonth;    double invest[12][12];    double totalMoney;    while(cin >> n && n!= -1)    {         for(int i=0; i<n; i++)             cin >> invest[i][0] >> invest[i][1];         cin >> terminalMonth >> totalMoney;         double l=1,r = 2, m;         while(r - l >= 1e-6)         {             m = (l+r)/2;   //求中值              double sum = 0;             for(int i=0; i<n; i++)             {                sum += invest[i][1]* pow(m,terminalMonth - invest[i][0]+1);                 }                  if(sum < totalMoney)                l = m;             else                r = m;         }                if(caseNum > 1)            printf("\n");         printf("Case %d: %0.5lf\n", caseNum++, l-1);            }    system("pause");    return 0;}

原创粉丝点击