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
 Copy sample input to clipboard 
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
这题用二分法求解就很简单,但是有一点要注意,就是它的输入是升序的,但不是单调增的,因为我的代码在直接用month做下标的时候是错的,然后改用数组储存月份就过了,说明它的月份是有重复的,也就是一个人在同一个月储存了几次。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std; const double eps = 1e-; int dcmp(double x){
return (x > -eps && x < eps) ? : ;
} int main(int argc, char const *argv[])
{
int N, month[];
double moneyIn[], lastMoney, lastMonth;
int testNum = ; while (cin >> N && N != -) {
memset(moneyIn, 0.0, sizeof(moneyIn));
for (int i = ; i < N; ++i) {
cin >> month[i];
cin >> moneyIn[i];
} cin >> lastMonth >> lastMoney;
double lastMoneyT = 0.0, rateE = 2.0, rateB = 1.0, rate = 1.5; lastMoneyT = 0.0;
for (int i = ; i < N; ++i)
lastMoneyT += moneyIn[i] * pow(rate, lastMonth - month[i] + ); while (dcmp(lastMoneyT - lastMoney) != && rateE - rateB >= 1e-) {
if (lastMoneyT > lastMoney)
rateE = rate;
else
rateB = rate; rate = (rateE + rateB) / ;
lastMoneyT = 0.0;
for (int i = ; i < N; ++i)
lastMoneyT += moneyIn[i] * pow(rate, lastMonth - month[i] + );
} if (++testNum > )
cout << endl;
printf("Case %d: %.5lf\n", testNum, rate - );
}
return ;
}

sicily 1017. Rate of Return的更多相关文章

  1. UVA 11881 Internal Rate of Return(数学+二分)

    In finance, Internal Rate of Return (IRR) is the discount rate of an investment when NPV equals zero ...

  2. UVA 11881 - Internal Rate of Return - [二分]

    依然是来自2017/9/17的周赛水题…… 题目链接:https://cn.vjudge.net/problem/UVA-11881 题解: 观察这个函数: 由于CF[i]固定值,因此NPV(IRR) ...

  3. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  4. 完成C++不能做到的事 - Visitor模式

    拿着刚磨好的热咖啡,我坐在了显示器前.“美好的一天又开始了”,我想. 昨晚做完了一个非常困难的任务并送给美国同事Review,因此今天只需要根据他们提出的意见适当修改代码并提交,一周的任务就完成了.剩 ...

  5. 【.net 深呼吸】自定义特性(Attribute)的实现与检索方法

    在.net的各个语言中,尤其是VB.NET和C#,都有特性这一东东,具体的概念,大家可以网上查,这里老周说一个非标准的概念——特性者,就是对象的附加数据.对象自然可以是类型.类型成员,以及程序集. 说 ...

  6. 让 OpenAL 也支持 S16 Planar(辅以 FFmpeg)

    正在制作某物品,现在做到音频部分了. 原本要采用 SDL2_mixer 的,不过实验结果表明其失真非常严重,还带有大量的电噪声.不知道是不是我打开的方式不对…… 一气之下去看 OpenAL,结果吃了闭 ...

  7. 【笔记6】用pandas实现条目数据格式的推荐算法 (基于物品的协同)

    ''' 基于物品的协同推荐 矩阵数据 说明: 1.修正的余弦相似度是一种基于模型的协同过滤算法.我们前面提过,这种算法的优势之 一是扩展性好,对于大数据量而言,运算速度快.占用内存少. 2.用户的评价 ...

  8. <更新日期03-31-2016> 复利计算5.0 <已改进>

    作业要求: 1.客户说:帮我开发一个复利计算软件. 完成复利公式计算程序,并成功PUSH到github上. 客户提出: 2.如果按照单利计算,本息又是多少呢? 3.假如30年之后要筹措到300万元的养 ...

  9. DataBinding examples

    Databinding in Windows Forms demo (CSWinFormDataBinding) /************************************* Modu ...

随机推荐

  1. 【BZOJ3555】企鹅QQ(字符串哈希)

    [BZOJ3555]企鹅QQ(字符串哈希) 题面 BZOJ 题解 把前缀哈希一下,后缀哈希一下 枚举哪个位置不选,然后检查一下相同就行了.. 为什么我的\(Hash\)老是\(WA\), 为什么\(Z ...

  2. 【BZOJ5248】【九省联考2018】一双木棋(搜索,哈希)

    [BZOJ5248][九省联考2018]一双木棋(搜索,哈希) 题面 BZOJ Description 菲菲和牛牛在一块n行m列的棋盘上下棋,菲菲执黑棋先手,牛牛执白棋后手.棋局开始时,棋盘上没有任何 ...

  3. Thuwc2018 游记

    上一次没有滚粗的比赛已经是9个月前了QAQ.但我现在回过头去看那篇“zjoi游记”,却发现自己并不能从中得到收获.希望这次写下的东西,可以帮助我更好地准备即将到来的省选(雾) day 0 火车上浪10 ...

  4. Insert Node in Sorted Linked List

    Insert a node in a sorted linked list. Have you met this question in a real interview?  Yes Example ...

  5. bzoj4010: [HNOI2015]菜肴制作(拓扑排序+贪心+堆)

    这题不是求最小字典序...撕烤了半个小时才发现不对劲T T 这题是能让小的尽量前就尽量前,无论字典序...比如1能在2前面就一定要在2前面... 显然是要先拓扑排序,让小的尽量前转化成让大的尽量往后丢 ...

  6. Flash平台的分析与RIA的趋势

    10月3号,Flash Player 11 和 AIR 3.0正式提供下载,一片安静.最近这两年来,关于Flash的新闻一向是以负面为主,先是 Silverlight 的挑战,然后是 iphone和i ...

  7. ls乱码问题解决

    http://note.youdao.com/noteshare?id=c7ff510525b4dadaabb6f6a0a72040cc

  8. python基础6--目录结构

    为什么要设计好目录结构? "设计项目目录结构",就和"代码编码风格"一样,属于个人风格问题.对于这种风格上的规范,一直都存在两种态度: 一类同学认为,这种个人风 ...

  9. 赤池信息量准则 ( Akaike information criterion)

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  10. centos7 samba匿名访问设置

    [global] workgroup = WORKGROUP server string = Samba Server %v netbios name = centos security = user ...