/*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;
}
1017. Rate of Return (用二分法求近似解)
最新推荐文章于 2021-06-26 11:10:04 发布