zcmu1757: 內部收益率【二分】

1757: 內部收益率

Description

Input

Output

Sample Input

1 -1 2 2 -8 6 9 0

Sample Output

1.00 0.50

HINT

 

解題思路:這個函數是單調的,所以不會too many;CF0<0,且IRR爲負數時要小於-1,也就是絕對值大於1,其他的CFi都大於0,所以一定有近似解,在求解的時候用的是二分迭代

#include<bits/stdc++.h>
using namespace std;
double cf[30];
int main(void)
{
	int i,j,n;
	double s,f,x,l,r,m;
	while(~scanf("%d",&n)&&n)
	{
		scanf("%lf",&x);
		for(i=0;i<n;i++)
			scanf("%lf",&cf[i]);
		l=-1.0;
		r=1e6;
		for(i=0;i<100;i++)
		{
			s=0;
			f=1.0;
			m=(l+r)/2;
			for(j=0;j<n;j++)
			{
				f=f/(1+m);
				s=s+cf[j]*f;
			}
			if(s>(-x))
				l=m;
			else
				r=m;
		}
		printf("%.2lf\n",m);
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章