12. The primary series save money problems

Save money problems

Problem Description

Lump assume different bank deposit interest rate of monthly interest period:

Period of 1 year 0.63%

0.66% 2-year period

0.69% three-year period

0.75% five-year period

0.84% ​​8-year period

Now known to have someone on hand 2,000 yuan deposit interest requirements make the money up to 20 years after the bank obtained by calculating save money by selecting one scenario, assume that bank deposits beyond the period of time that part does not pay interest

problem analysis

In order to get the most interest, should be taken immediately after the expiry of money out of the bank, and then immediately to the original principal amount plus the current interest acquired as the new capital in the bank, so that repeated operation until life for 20 years until the deposits due to the different duration of the corresponding rates are not the same, so in 20 years, the interest of different combinations of the obtained access period is not the same assuming that 20 years, 1 year period the time saved x1, x2 keep the 2 year period of time, keep the 3-year period of time x3, x5 times saved five-year period, saved x8 time period of eight years, while the income of depositors maturity total principal and interest are:

2000 (1 + 0.063) ^ X1 (1 + 0.066) ^ X2 (1 + 0.069) ^ X3 (1 + 0.075) ^ X5 (1 + 0.084) Formula 1 x8 ^

Obviously the number of 8-year period deposits up to two times, so to obtain the following restriction condition of deposit term:

​ 0 <= x8 <= 2

​ 0 <= x5 <= (20 - 8*x8) / 5

​ 0 <= x3 <= (20 - 8x8-5x5)/3

​ 0<= x2 <= (20 - 8x8 - 5x5 -3X3)/2

​ x1 = 20 - 8x8-5x5-3x3-2x2 and x1 >= 0

algorithm design

According to Equation 1 and the qualifications of the deposit period, you can use a for loop to exhaustive of all possible to find out the amount of the deposit is the solution of the problem of the maximum amount of the deposit

#include <stdio.h>
#include <math.h>

int main(void)
{
    int x1, x2, x3, x4, x5, x8;
    int y1, y2, y3, y5, y8;
    double max = 0.0, result;       /*!<result 变量存放最后结果*/
    /* !<使用for循环穷举所有可能存款方案 */
    for (x8 = 0; x8 <= 2;x8++) {
        for (x5 = 0; x5 <= (20 - 8*x8) / 5; x5++) {
            for (x3 = 0; x3 <= (20 - 8*x8 - 5*x5) / 2; x3++) {
                for (x2 = 0; x2 <= (20 - 8*x8 - 5*x5 - 3*x3) / 2; x2++) {
                    x1 = 20 - 8*x8 - 5*x5 - 3*x3 - 2*x2;    /* !< 存款期限限定条件 */
                    /* !< 判断条件 */
                    result = 2000.0*pow((1+0.0063*12), x1)
                                   *pow((1+2*0.0066*12), x2)
                                   *pow((1+3*0.0069*12), x3)
                                   *pow((1+5*0.0075*12), x5)
                                   *pow((1+8*0.0084*12), x8);
                    /* !< y1, y2, y3, y8用于记录获利最多的存放方式*/
                    if (result > max) {
                        max = result;   /* !< max变量存放当前的最大值*/
                        y1 = x1;
                        y2 = x2;
                        y3 = x3;
                        y5 = x5;
                        y8 = x8;
                    }
                }
            }
        }
    }
    /* !< 输出结果 */
    printf("获得利息最多的存款方式为:\n");
    printf("8年期限的存了%d 次\n", y8);
    printf("5年期限的存了%d 次\n", y5);
    printf("3年期限的存了%d 次\n", y3);
    printf("2年期限的存了%d 次\n", y2);
    printf("1年期限的存了%d 次\n", y1);
    printf("存款人最终的获得的本利合计: %0.2f\n", result);
}

/* !< pow function introduce */
/* double pow(double x, double y) */
/* function 计算x^y的值 */

/* !< output */
    获得利息最多的存款方式为:
    8年期限的存了0 次
    5年期限的存了4 次
    3年期限的存了0 次
    2年期限的存了0 次
    1年期限的存了0 次
    存款人最终的获得的本利合计: 8763.19

    Process returned 0 (0x0)   execution time : 0.005 s
    Press any key to continue.

Guess you like

Origin www.cnblogs.com/xuzhaoping/p/11484583.html