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. bzoj5039:[Jsoi2014]序列维护

    做做bzoj上的新题(不存在的) 同bzoj1798: [Ahoi2009]维护序列,样例都一样的...我能想象到的唯一的新的考察意义就是模数是2e9不是1e9,于是加法的时候需要转long long ...

  2. 【JavaScript】JAVA-js中比较日期大小

    业务场景:js中根据yyyy-MM-dd格式的日期进行比较来动态显示相关图标的出现与否 var DS173305 = { DS173305Grid: null, initDataGrid: funct ...

  3. (三)MySQL学习笔记

    [Leecode]175. 组合两个表 解答:由于是组合两个表的信息,很容易想到连接查询,这里使用左连接 select p.Firstname,p.Lastname,q.City,q.State fr ...

  4. [JLOI2011]飞行路线 最短路

    题面 题面 题解 这题不是很难,因为删代价的次数不多,因此我们只需要将最短路中的状态加一维表示已经删了几次,再转移即可 #include<bits/stdc++.h> using name ...

  5. 【数据结构】【平衡树】treap

    之前写treap的传送门 之前写的那个太毒瘤了,这次放一个更毒瘤的指针版上来 #include<cstdio> #include<iostream> #define rg re ...

  6. bzoj1467 Pku3243 clever Y

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 313  Solved: 181[Submit][Status ...

  7. HDU 6199 DP

    gems gems gems Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. WPF系列之一:基于并行任务和MVVM创建响应灵敏和数据驱动的UI

    在利用WPF创建桌面应用程序的界面时,经常使用MVVM的设计模式,以减少UI层与逻辑层的代码耦合度. 在MVVM的设计中,最主要的方法和技术是UI中的控件利用Binding来和逻辑层(ViewMode ...

  9. Vue.js随笔二(新建路由+component+添加样式+变量的显示)

    创建一个页面: 1.首先让我们看一下整个vue.js的目录,如下图所示: 2.现在让我们创建一个页面吧: 2-1首先你需要新建路由(就和建立一个如何找到项目文件的目录一个意思):进入src/route ...

  10. 【转】手摸手,带你用vue撸后台 系列二(登录权限篇)

    前言 拖更有点严重,过了半个月才写了第二篇教程.无奈自己是一个业务猿,每天被我司的产品虐的死去活来,之前又病了一下休息了几天,大家见谅. 进入正题,做后台项目区别于做其它的项目,权限验证与安全性是非常 ...