[Sicily online] 1017. Rate of Return

Source: Internet
Author: User
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 investigation has varied, and she hasn't always added to the investigation 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 wowould 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 wocould 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 wowould
Have been paid had she invested in a savings account wowould have been paid at the end of the month, and wowould 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 each l 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 wocould have been 100 × (1 + I ). At the end of February the value wocould have been 100 × (1 + I ) × (1 + I ), Or 100 × (1 + I ) 2. At the end of March,
The value wocould have been100 × (1 + I ) 3 + 100 × (1 +I ), And at the end of each L, the value wocould have been100 × (1 + I ) 4 + 100 × (1 + I ) 2. So the question to be answered in this case is
This: what is the value I Such that 100 × (1 + I ) 4 + 100 × (1 + I 2 = 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 investors 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 wowould have paid. display this number with five fractional digits, rounded
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.01635 Case 2: 0.00520

Question Analysis:

This question is to solve the equation. You can use the bipartite method or the Newton iteration method. I use the bipartite method.

/**/# Include <iostream> # include <iomanip> # include <stdio. h> # include <cmath> # include <iomanip> # include <list> # include <map> # include <vector> # include <string> # include <algorithm> # include <sstream> # include <stack> # include <queue> # include <string. h> using namespace STD; typedef struct Monthe {int index; double price;} mouth; int main () {int N; int COUNT = 0; while (CIN> N & n! =-1) {If (count! = 0) cout <Endl; count ++; vector <mouth> data (n); mouth lastmouth; For (INT I = 0; I <n; I ++) cin> data [I]. index> data [I]. price; CIN> lastmouth. index> lastmouth. price; double front = 1.0, last = 2.0, mid = 1.5; while (1) {mid = (front + last)/2; double sum = 0; for (INT I = 0; I <n; I ++) {sum + = POW (MID, lastmouth. index-data [I]. index + 1) * Data [I]. price;} If (FABS (sum-lastMouth.price) <1e-6) break; else if (sum <lastmouth. price) Front = mid; else last = mid;} cout <"case" <count <":"; cout <setiosflags (IOs: fixed) <setprecision (5) <mid-1 <Endl ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.