sicily 1017. Rate of Return
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.
2 1 100.00 3
100.00 4 210.00 3
1 100.00
2 50.00
5 200.00
7 358.41 -1
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的更多相关文章
- UVA 11881 Internal Rate of Return(数学+二分)
In finance, Internal Rate of Return (IRR) is the discount rate of an investment when NPV equals zero ...
- UVA 11881 - Internal Rate of Return - [二分]
依然是来自2017/9/17的周赛水题…… 题目链接:https://cn.vjudge.net/problem/UVA-11881 题解: 观察这个函数: 由于CF[i]固定值,因此NPV(IRR) ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- 完成C++不能做到的事 - Visitor模式
拿着刚磨好的热咖啡,我坐在了显示器前.“美好的一天又开始了”,我想. 昨晚做完了一个非常困难的任务并送给美国同事Review,因此今天只需要根据他们提出的意见适当修改代码并提交,一周的任务就完成了.剩 ...
- 【.net 深呼吸】自定义特性(Attribute)的实现与检索方法
在.net的各个语言中,尤其是VB.NET和C#,都有特性这一东东,具体的概念,大家可以网上查,这里老周说一个非标准的概念——特性者,就是对象的附加数据.对象自然可以是类型.类型成员,以及程序集. 说 ...
- 让 OpenAL 也支持 S16 Planar(辅以 FFmpeg)
正在制作某物品,现在做到音频部分了. 原本要采用 SDL2_mixer 的,不过实验结果表明其失真非常严重,还带有大量的电噪声.不知道是不是我打开的方式不对…… 一气之下去看 OpenAL,结果吃了闭 ...
- 【笔记6】用pandas实现条目数据格式的推荐算法 (基于物品的协同)
''' 基于物品的协同推荐 矩阵数据 说明: 1.修正的余弦相似度是一种基于模型的协同过滤算法.我们前面提过,这种算法的优势之 一是扩展性好,对于大数据量而言,运算速度快.占用内存少. 2.用户的评价 ...
- <更新日期03-31-2016> 复利计算5.0 <已改进>
作业要求: 1.客户说:帮我开发一个复利计算软件. 完成复利公式计算程序,并成功PUSH到github上. 客户提出: 2.如果按照单利计算,本息又是多少呢? 3.假如30年之后要筹措到300万元的养 ...
- DataBinding examples
Databinding in Windows Forms demo (CSWinFormDataBinding) /************************************* Modu ...
随机推荐
- BZOJ 1216 操作系统(堆)
用堆模拟题目中的操作即可. # include <cstdio> # include <cstring> # include <cstdlib> # include ...
- Best Reward HDU - 3613(马拉车+枚举+前缀和)
题意: 给你一串字符串,每个字符都有一个权值,要求把这个字符串在某点分开,使之成为两个单独的字符串 如果这两个子串某一个是回文串,则权值为那一个串所有的字符权值和 若不是回文串,则权值为0 解析: 先 ...
- Period UVALive - 3026(next数组)
题意: 给出一个长度不超过1000000的字符串S, 对于该字符串的所有前缀求其周期, 如果周期K >= 2输出起始位置是第几个字符和其周期K 解析: 先求next数组 对于每一个位置如果i % ...
- 学Python Django学得很迷茫,怎么办?-转自知乎
本人学生,零编程基础,在学习python的过程中越学越迷茫,感觉像无头苍蝇一样,来知乎取经,下面进入正题吧: 我是先看了中谷的python教学视频,然后跟着慕课网上的python教程把题 ...
- Python下json中文乱码解决办法
json.dumps在默认情况下,对于非ascii字符生成的是相对应的字符编码,而非原始字符,只需要 #coding=utf8 import json js = json.loads('{" ...
- 洛谷 P2195 HXY造公园 解题报告
P2195 HXY造公园 题目描述 现在有一个现成的公园,有\(n\)个休息点和\(m\)条双向边连接两个休息点.众所周知,\(HXY\)是一个\(SXBK\)的强迫症患者,所以她打算施展魔法来改造公 ...
- SQL中的替换函数replace()使用
语法REPLACE ( string_expression , string_pattern , string_replacement ) 参数string_expression 要搜索的字符串表达式 ...
- NOIP2017 Day2 T3 列队(treap)
可以直接用treap上大模拟...n+1个treap维护n行的前m-1个点和最后一列. 需要支持删除一个点或者一段区间,而空间并不支持存下所有的点的时候,可以用一个点代替一个区间,记录区间首项的值和区 ...
- 关于JBoss基本说明文档及基本使用安装
关于JBoss JBoss是全世界开发者共同努力的成果,一个基于J2EE的开放源代码的应用服务器.在不 到12个月的时间里有一百万以上的拷贝被下载.JBoss是第一位的J2EE应用服务器. J ...
- selenium - javascript - 滚动条
虽然WebDriver提供了操作浏览器的前进和后退方法,但对于浏览器滚动条并没有提供相应的操作方法.在这种情况下,就可以借助JavaScript来控制浏览器的滚动条.WebDriver提供了execu ...