sicily 1017. Rate of Return

原创 2015年11月17日 15:57:20

1017. Rate of Return

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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

sicily题目分类

sicily题目分类 1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. ...
  • u010858667
  • u010858667
  • 2013年09月05日 11:52
  • 2147

Sicily 1135 飞越原野

广度优先搜索,三维数组判重 #include #include #include using namespace std; struct Node { int x,y,p; }; ch...
  • Detective_Xin
  • Detective_Xin
  • 2013年11月22日 11:19
  • 1316

[sicily]部分题目分类

*************************程序设计题************************* sicily 1293, uva 100, pc 110101, 3n+1数链问题, 难...
  • luohuang0423
  • luohuang0423
  • 2015年09月24日 11:20
  • 778

中山大学Sicily在线测评系统题目分类

Classified Problems on Online Judge 练习题选自以下在线测评系统 * sicily: http://soj.me, 中山大学Sicily在线测评系统 ...
  • cuhkljt
  • cuhkljt
  • 2013年06月26日 21:39
  • 4824

sicily 简单哈希

sicily 1004. 简单哈希 原题: Description  使用链地址法(又称拉链法)可以解决Hash中的冲突问题。其基本思想是:将具有相同哈希地址的记录链成一个单链表,m个哈希地址就...
  • t6_17
  • t6_17
  • 2016年12月14日 21:26
  • 549

1017. A除以B (20):做完挺开心的一道=.=

总结(吐槽):算法不是很清晰 思路就是 简单模拟手算除法  模拟过程中想到用堆栈 后来调试过程中发现多此一举                           之所以开心 是因为 做了三个小时 期间...
  • qq_17249047
  • qq_17249047
  • 2015年07月02日 10:30
  • 1298

Sicily求A-B(也即求A对B的差集)

题目描述思路介绍:①使用vector存储AB ②使用unique与erase对AB分别进行去重 ③开辟一个标记数组,使用二重循环...
  • wyxwyx469410930
  • wyxwyx469410930
  • 2017年12月22日 00:29
  • 57

sicily 1215 脱离地牢

做Sicily 1215脱离地牢 这一题,一直Restrict function,都快崩溃了。 最后发现是内存泄露,就是new出来的东西没有及时delete导致的。 具体是在广度优...
  • chz429
  • chz429
  • 2014年12月27日 10:25
  • 626

sicily 1019(动态规划)

题目链接:sicily 1019 题目分析:恶心的一笔的一道题,不过确实是一道好题。给你一棵树,n个结点,结点间距离为1,每个结点有一个值,第一次走过某个节点获得该结点的值,问用m步遍历树可以获得值...
  • fuyukai
  • fuyukai
  • 2015年04月18日 07:19
  • 394

sicily 1156之约定俗成

这是题目,我就不黏贴了点击打开链接
  • xujqiao
  • xujqiao
  • 2014年07月24日 12:08
  • 296
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:sicily 1017. Rate of Return
举报原因:
原因补充:

(最多只允许输入30个字)