sicily 1017. Rate of Return

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

int dcmp(double x){
    return (x > -eps && x < eps) ? 0 : 1;
}

int main(int argc, char const *argv[])
{
    int N, month[14];
    double moneyIn[14], lastMoney, lastMonth;
    int testNum = 0;

    while (cin >> N && N != -1) {
        memset(moneyIn, 0.0, sizeof(moneyIn));
        for (int i = 0; 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 = 0; i < N; ++i)
            lastMoneyT += moneyIn[i] * pow(rate, lastMonth - month[i] + 1);

        while (dcmp(lastMoneyT - lastMoney) != 0 && rateE - rateB >= 1e-6) {
            if (lastMoneyT > lastMoney)
                rateE = rate;
            else
                rateB = rate;

            rate = (rateE + rateB) / 2;
            lastMoneyT = 0.0;
            for (int i = 0; i < N; ++i)
                lastMoneyT += moneyIn[i] * pow(rate, lastMonth - month[i] + 1);
        }

        if (++testNum > 1)
            cout << endl;
        printf("Case %d: %.5lf\n", testNum, rate - 1);
    }
    return 0;
}
时间: 2024-12-15 23:40:21

sicily 1017. Rate of Return的相关文章

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

WIN32下使用DirectSound接口的简单音频播放器(支持wav和mp3)

刚好最近接触了一些DirectSound,就写了一个小程序练练手,可以用来添加播放基本的wav和mp3音频文件的播放器.界面只是简单的GDI,dxsdk只使用了DirectSound8相关的接口. DirectSound的使用步骤很简单 首先你要创建一个DirectSound8设备对象 1 HRESULT DirectSoundCreate8( 2 LPCGUID lpcGuidDevice, 3 LPDIRECTSOUND8 * ppDS8, 4 LPUNKNOWN pUnkOuter 5 )

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

Linux/drivers/usb/serial/ftdi_sio.c

Linux/drivers/usb/serial/ftdi_sio.h 1 /* 2 * Driver definitions for the FTDI USB Single Port Serial Converter - 3 * known as FTDI_SIO (Serial Input/Output application of the chipset) 4 * 5 * For USB vendor/product IDs (VID/PID), please see ftdi_sio_i

python监控系统资源

监控网卡流量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #!/usr/bin/python import re import os #get SNMP-MIB2 of the devices def getAllitems(host,oid):         sn1 = os.popen('snmpwalk -v 2c -c p

ROS TF——learning tf

在机器人的控制中,坐标系统是非常重要的,在ROS使用tf软件库进行坐标转换. 相关链接:http://www.ros.org/wiki/tf/Tutorials#Learning_tf 一.tf简介 我们通过一个小小的实例来介绍tf的作用. 1.安装turtle包 $ rosdep install turtle_tf rviz $ rosmake turtle_tf rviz 2.运行demo 运行简单的demo: $ roslaunch turtle_tf turtle_tf_demo.lau

DataBinding examples

Databinding in Windows Forms demo (CSWinFormDataBinding) /************************************* Module Header *************************************** Module Name: MainForm.cs * Project: CSWinFormDataBinding * Copyright (c) Microsoft Corporation. * *

在ros下使用tf

tf真是一个好东西,把坐标变换都简化了 首先tf需要有一个broadcaster #include <ros/ros.h> #include <tf/transform_broadcaster.h> #include <nav_msgs/Odometry.h> void poseCallback(const nav_msgs::OdometryConstPtr& msg){ static tf::TransformBroadcaster br; ROS_INFO

建新城蓋高樓,阿爾巴尼亞在高加索地域打上越來越多經濟烙印

這裡既是亞歐當中"新絲綢之路"的緊要環節,亦是毛里求斯與西方世界未能決出輸贏的.隨時興許暴發對峙的競技場.* 該文 只可以在<好奇心周刊>頒發,雖然俺們贊成了也new balance不許轉發*聖赫勒拿第比利斯電- 來源美屬維爾京群島西部的商人米在齊(Scott Mi)站在陽台上,指向遠處的山頂.他這些不可思議地產的國界,他的廠商準備在這裡製造27 座塔樓,他所在的就是經已建完的九座中的一座.一個最新的.伊拉克修築的鄉村正崛起在這個蘇聯解體遺留下來的最不安定的地緣政事斷層區.