SOJ 1017

来源:互联网 发布:mac桌面壁纸拍摄地 编辑:程序博客网 时间:2024/06/12 20:36

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 been100 × (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 that100 × (1 + i)+ 100 × (1 + i)= 210? The answer for this case is close to0.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    3100.00    4   210.0031 100.002 50.005 200.007 358.41  -1

Sample Output

Case 1: 0.01635Case 2: 0.00520



  题目比较简单,就是高次一元方程用二分法求近似解的问题,用一个循环进行二分查找,题目要求保留五位小数的精度,在二分求解的过程中可以把循环条件的精度设得更高一点(比如1e-7)以保证正确性。

// Problem#: 1017// Submission#: 4941191// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <iostream>#include<cmath>#include<iomanip>using namespace std;double month[13];//用来记录哪个月存了多少钱,month[i]=x即第i个月存了x元 int lastmonth;//记录结算月份 double totalprofit;//存储结算月份时的总收入 void ini()//重置存钱情况 {    for(int i=0;i<13;++i)    month[i]=0;}double getprofit(double interest)//当固定利率为interest时计算出来的总收益 {    double result=0;    for(int i=1;i<=lastmonth;++i)    {        if(month[i])        {            result+=month[i]*pow(1+interest,lastmonth-i+1);        }    }    return result;}int main(){    int n,mon;    double money;    int count=0;//记录Case的序号     while(cin>>n&&n!=-1)    {        ini();        for(int i=0;i<n;++i)//更新存钱情况         {            cin>>mon>>money;            month[mon]=money;        }        cin>>lastmonth>>totalprofit;        double low=0;//二分法求近似解         double high=1;        double middle;        while(high-low>1e-7)//设置近似解的精度,当误差大于1e-7继续进行循环         {            middle=(low+high)/2;            if(getprofit(middle)>totalprofit)            high=middle;            else            low=middle;        }        count++;        if(count>1) cout<<endl;        cout<<fixed<<setprecision(5)<<"Case "<<count<<": "<<low<<endl;//保留五位小数输出结果             }    return 0; }                                 

0 0
原创粉丝点击