134人阅读 评论(0)

# Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

# Description

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)+ 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)+ 100 × (1 + i)= 210? The answer for this case is close to 0.016351795234.

# Input

The input from file i.in 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.

# Output

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.

# Sample Input

2   1   100.00    3
100.00    4   210.00

3
1 100.00
2 50.00
5 200.00
7 358.41

-1

# Sample Output

Case 1: 0.01635

Case 2: 0.00520

#include <iostream>
#include <cmath>
#include <iomanip>
#define ESP 1e-6

struct Investment {
int time;
double corpus;
};

int main(){
int num;
int id = 1;
while (std::cin >> num && num != -1) {
Investment in[12];
for (int c = 0; c < num; ++c)
std::cin >> in[c].time >> in[c].corpus;

int end;
double money;
std::cin >> end >> money;

double high = 1;
double low = 0;
while (high - low > ESP) {
double mid = (high + low) / 2;
double sum = 0;
for (int c = 0; c < num; ++c) {
sum += in[c].corpus * pow(1 + mid, end - in[c].time + 1);
}
if (sum < money) {
low = mid;
} else {
high = mid;
}
}
if (id != 1)
std::cout << std::endl;
std::cout << "Case " << id++ << ": "
<< std::showpoint << std::fixed
<< std::setprecision(5) << high << std::endl;
}
return 0;
}

0
0

【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【套餐】深度学习入门视频课程——唐宇迪
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】计算机视觉原理及实战——屈教授
【直播】机器学习之凸优化——马博士
【直播】机器学习&数据挖掘7周实训--韦玮

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：23459次
• 积分：1241
• 等级：
• 排名：千里之外
• 原创：104篇
• 转载：0篇
• 译文：1篇
• 评论：3条
文章分类
阅读排行
评论排行
最新评论