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 ...
随机推荐
- Qt中父子widget的事件传递
以前我一直以为:在父widget上摆一个子widget后,当click子widget时:只会进入到子widget的相关事件处理函数中,比如进入到mousePressEvent()中, 而不会进入到父w ...
- @Retention(保留) 此注解用于运行时候(反射)时候使用 如果不使用的话 在反射时候无法获取到注解的值
@Retention(保留) 此注解用于运行时候(反射)时候使用 如果不使用的话 在反射时候无法获取到注解的值
- 在 Android开发中,性能优化策略十分重要
在 Android开发中,性能优化策略十分重要本文主要讲解性能优化中的布局优化,希望你们会喜欢.目录 示意图 1. 影响的性能 布局性能的好坏 主要影响 :Android应用中的页面显示速度 2. 如 ...
- C++操作Windows WIFI
原文链接地址:https://blog.csdn.net/just_do_1122/article/details/78031024 实现功能 无线网卡列表 无线热点扫面 无线 ...
- Kerberos的黄金票据详解
0x01黄金票据的原理和条件 黄金票据是伪造票据授予票据(TGT),也被称为认证票据.如下图所示,与域控制器没有AS-REQ或AS-REP(步骤1和2)通信.由于黄金票据是伪造的TGT,它作为TGS- ...
- yd的拔钉子之路之 POI 2017
写在前面的一些话 如果我NOIP没退役,这大概会写成一个系列吧,所以这算是系列的开始,要写一些奇怪的东西? 首先解释下什么叫“拔钉子”,其实就是在钉子上做题嘛......至于钉子具体是个什么东西就当面 ...
- [Wf2011]Chips Challenge
两个条件都不太好处理 每行放置的个数实际很小,枚举最多放x 但还是不好放 考虑所有位置先都放上,然后删除最少使得合法 为了凑所有的位置都考虑到,把它当最大流 但是删除最少,所以最小费用 行列相关,左行 ...
- day2-python基础
- 《剑指offer》— JavaScript(10)矩形覆盖
矩形覆盖 题目描述 我们可以用(2*1)的小矩形横着或者竖着去覆盖更大的矩形.请问用n个(2*1)的小矩形无重叠地覆盖一个(2*n)的大矩形,总共有多少种方法? 实现代码 function jumpF ...
- PID控制算法的C语言实现十 专家PID与模糊PID的C语言实现
本节是PID控制算法的C语言实现系列的最后一节,前面8节中,已经分别从PID的实现到深入的过程进行了一个简要的讲解,从前面的讲解中不难看出,PID的控制思想非常简单,其主要问题点和难点在于比例.积分. ...