POJ-2199 Rate of Return【二分求解一元高次方程】
来源:互联网 发布:怎么退出手机淘宝账号 编辑:程序博客网 时间:2024/05/17 02:12
Jill has been investing in a mutual fund for a while. Since her income has varied, the amount of money she has added to the investment has varied, and she hasn’t always added to the investment at regular intervals. Nevertheless, she does have a complete record of the amounts she has invested, and the dates of those investments.
Periodically Jill gets a report that indicates the total value of her investment. She wonders if she would have done better by investing her money in a savings account that pays a fixed interest rate. But to determine the answer to this question, she needs to know what the equivalent interest rate would have been paid on the mutual fund, had it paid a fixed rate. You are going to help her.
For simplicity we will assume that Jill added money to her mutual fund only at the beginning of a month, and that all months have the same length. We will further assume that the interest she would have been paid had she invested in a savings account would have been paid at the end of the month, and would have been compounded monthly.
Let's consider a simple example. Suppose Jill invested $100 at the beginning of January and another $100 in March. At the end of April she finds that the value of her mutual fund is $210. If the equivalent fixed monthly interest rate was i, then we know that at the end of January the value would have been 100 * (1 + i). At the end of February the value would have been 100 * (1 + i) * (1 + i), or 100 * (1 + i)2. At the end of March, the value would have been 100 * (1 + i) 3 + 100 * (1 + i), and at the end of April, the value would have been 100 * (1 + i) 4 + 100 * (1 + i)2. So the question to be answered in this case is this: what is the value of i such that 100 * (1 + i) 4 + 100 * (1 + i) 2 = 210? The answer for this case is close to 0.016351795234.
Periodically Jill gets a report that indicates the total value of her investment. She wonders if she would have done better by investing her money in a savings account that pays a fixed interest rate. But to determine the answer to this question, she needs to know what the equivalent interest rate would have been paid on the mutual fund, had it paid a fixed rate. You are going to help her.
For simplicity we will assume that Jill added money to her mutual fund only at the beginning of a month, and that all months have the same length. We will further assume that the interest she would have been paid had she invested in a savings account would have been paid at the end of the month, and would have been compounded monthly.
Let's consider a simple example. Suppose Jill invested $100 at the beginning of January and another $100 in March. At the end of April she finds that the value of her mutual fund is $210. If the equivalent fixed monthly interest rate was i, then we know that at the end of January the value would have been 100 * (1 + i). At the end of February the value would have been 100 * (1 + i) * (1 + i), or 100 * (1 + i)2. At the end of March, the value would have been 100 * (1 + i) 3 + 100 * (1 + i), and at the end of April, the value would have been 100 * (1 + i) 4 + 100 * (1 + i)2. So the question to be answered in this case is this: what is the value of i such that 100 * (1 + i) 4 + 100 * (1 + i) 2 = 210? The answer for this case is close to 0.016351795234.
The input will contain multiple cases. The input for each case will begin with an integer N (no larger than 12) that indicates the number of times Jill invested in her mutual fund. This will be followed by N + 1 pairs, each pair containing an integer and a real number. The integer represents a month number (1 or larger) and the real number represents a dollar amount. The first N pairs give the month and amount of each of Jill’s N investments in the mutual fund, and the last pair indicates the value of the investment at the end of the specified month. There will be one or more whitespace characters (blanks, tabs, and/or ends of lines) between the input numbers. You may assume that the month numbers are given in ascending order.
Input for the last case will be followed by a single integer –1.
Input for the last case will be followed by a single integer –1.
For each case, display the case number (they start with 1 and increase sequentially) and the equivalent fixed monthly interest rate Jill's mutual fund would have paid. Display this number with five fractional digits, rounded to the nearest decimal place. You may assume the interest rate will be no less than 0 and no larger than 1. Separate the output for consecutive cases by a blank line.
2 1 100.00 3100.00 4 210.0031 100.002 50.005 200.007 358.41 -1
Case 1: 0.01635Case 2: 0.00520
题意:
jill每隔几个月会在一个固定利率的基金上投资一部分钱。每月会产生一定的利息。现在已知某月底jill账户上的金额,求利率是多少(0~1)。
思路:
一元高次方程的求解。可以用二分法无限逼近正解。有一个坑点,就是输出时需要用%.05f,如果是零的话,小数点后需要输出5个0。
代码:
#include<stdio.h>#include<string.h>#include<math.h>using namespace std;#define exp 0.000001double ans,dis[15],val;int n,power[15],end;double cal(double p){ double ret=0; for(int i=0;i<n;i++) ret+=dis[i]*pow(p,end-power[i]+1); return ret;}double bin(double L,double R){ if(R-L<exp) return L; double mid=(L+R)/2; double get=cal(mid); //printf("->%.5f %.5f %.5f\n",L,R,get); if(get>=val) return bin(L,mid); else return bin(mid,R);}int main(){ int cas=1; while(scanf("%d",&n) && n!=-1) { for(int i=0;i<n;i++) scanf("%d%lf",&power[i],&dis[i]); scanf("%d%lf",&end,&val); if(cas>1) printf("\n"); printf("Case %d: %.05f\n",cas++,bin(1,2)-1); } return 0;}
阅读全文
0 0
- POJ-2199 Rate of Return【二分求解一元高次方程】
- POJ-2199(一元n次方程,二分答案)(Rate of Return )
- 一元高次方程求解
- 一元高次方程的求解
- 一元高次方程的求解
- 二分逼近/牛顿迭代——一元高次非线性方程求解
- HDU 2199(牛顿迭代法)/(二分)(高次方程求解)
- POJ 3243 离散对数-高次同余方程求解
- noip2001一元三次方程求解(二分)
- 算法训练 一元三次方程求解 二分
- POJ 2417 && POJ 3243 求解高次同余方程 BSGS
- 求解一元四次方程
- 一元三次方程求解
- 求解一元四次方程
- 一元三次方程求解
- 一元三次方程求解
- 一元三次方程求解
- 一元三次方程求解
- 无向图邻接表改为邻接矩阵
- XYNUOJ 整除的尾数
- 输入一个3位整数,编程计算并输出它的逆序数(例:输入-123,则忽略负号,由123分离出其百位1,十位2,个位3,并计算输出321)
- 顺序表应用6:有序顺序表查询
- 习题1
- POJ-2199 Rate of Return【二分求解一元高次方程】
- 习题3
- codeforces Round House(在环上走的步数最终停到哪里)
- [Numpy] 数组数据的存储和管理
- 数码相框设计-文字编码方式
- 匈牙利算法
- graph
- jQuery的简单特效
- 秘钥认证、远程登录