sicily 1017. Rate of Return

//求解一元多次方程,注意到"the interest rate will be no less than 0 and no larger than 1."
//所以方程的输出是随着the value of i 严格增加,故可以采用二分查找方程的解

#include
<iostream>
#include
<stdio.h>
#include
<math.h>
using namespace std;
double fac[20],power[20],s,month;
int main()
{
int n,id=1;
while(cin>>n&&n!=-1)
{
for(int i=1;i<=n;++i)
cin
>>power[i]>>fac[i];
cin
>>month>>s;
double l=1,r=2,m;

//这里如果用le-5,则需要验证l,r,m哪个更靠近真解;而如果采用小一位的精度,则无论l,r,m 都可以通过
while(r-l>=1e-6)
{
m
=(r+l)/2;
double sum=0;
for(int i=1;i<=n;++i)
sum
+=fac[i]*pow(m,month-power[i]+1);
if(sum<s)
l
=m;
else
r
=m;
}
if(id>1)
printf(
"\n");
printf(
"Case %d: %.5f\n",id++,l-1);
}
return 0;
}

posted on 2011-07-04 12:23  sysu_mjc  阅读(364)  评论(0编辑  收藏  举报

导航