简体   繁体   English

递归程序

[英]recursive program

I am trying to make a recursive program that calculates interest per year.It prompts the user for the startup amount (1000), the interest rate (10%)and number of years(1).(in brackets are samples) 我正在尝试制作一个递归程序来计算每年的利息,它会提示用户输入启动金额(1000),利率(10%)和年数(1)。(括号内为示例)

Manually I realised that the interest comes from the formula YT(1 + R)----- interest for the first year which is 1100. 手动地,我意识到利息来自公式YT(1 + R)-----第一年的利息是1100。

2nd year YT(1 + R/2 + R2/2) //R squared 第二年YT(1 + R / 2 + R2 / 2)// R平方

2nd year YT(1 + R/3 + R2/3 + 3R3/) // R cubed 第二年YT(1 + R / 3 + R2 / 3 + 3R3 /)// R立方

How do I write a recursive program that will calculate the interest? 如何编写一个计算利息的递归程序? Below is the function which I tried 下面是我尝试过的功能

//Latest after editing //编辑后最新

double calculateInterest2(double start, double rate, int duration) 
{ 
    if (0 == duration) { 
        return start; 
    } else { 
        return (1+rate) * calculateInterest2(start, rate, duration - 1); 
    } 
} 

I took the liberty of testing your function in Java (the syntax is similar), and it returned weird results. 我随意使用Java测试您的函数(语法相似),并且返回了奇怪的结果。 Here is what I got: 这是我得到的:

calculateInterest2(1000, .1, 0); // = 1000.0
calculateInterest2(1000, .1, 1); // = 1200.0
calculateInterest2(1000, .1, 2); // = 1420.0
calculateInterest2(1000, .1, 3); // = 1662.0
calculateInterest2(1000, .1, 4); // = 1928.2

Obviously, this is not right. 显然,这是不对的。 First of all, the return line is also re-applying the calculation.... Here is a rewrite of the method: 首先,返回行也正在重新应用计算。...这是方法的重写:

static private double calculateInterest2(double start, double rate, int duration)
{
    if (0 == duration) {
        return start;
    } else {
        return (1+rate) * calculateInterest2(start, rate, duration - 1);
    }
}

As you can see, this method checks out with this output : 如您所见,此方法通过以下输出进行检出:

calculateInterest2(1000, .1, 0); // = 1000.0
calculateInterest2(1000, .1, 1); // = 1100.0
calculateInterest2(1000, .1, 2); // = 1210.0
calculateInterest2(1000, .1, 3); // = 1331.0
calculateInterest2(1000, .1, 4); // = 1464.1000000000001

Sounds more right to me. 听起来对我来说更正确。

Basically, a recursive function F works by calculating the answer for F(N) off of the answer for F(N-1) plus the extra work for the Nth case. 基本上,递归函数F通过从F(N-1)的答案计算F(N)的答案加上第N个情况的额外功来工作。 And of course you have to supply the terminating result for N==0 (or 1 or whatever) 当然,您必须提供N == 0(或1或其他值)的终止结果

So in your case, the compound interest for year N would be: 因此,在您的情况下,第N年的复利为:

  Interest(S,R,N) = (S + Interest(S,R,N-1)) * R

where 哪里

  Interest(S,R,0) = S * R

And you should be able to change your code to represent that pretty easily. 而且您应该能够更改代码以使其很容易地表示出来。

You forgot to decrement duration , so you end up with an infinite loop (or more likely in case of a recursion, a stack overflow). 您忘了减少duration ,所以最终会遇到无限循环(或者在递归的情况下更可能是堆栈溢出)。
You also do nothing with the result of calculateInterest , so you're returning the value for the first year: 您也不会对calculateInterest的结果做任何事情,因此您将返回第一年的值:

return calculateInterest(cpdInterest, rate, duration - 1);

Also, you may want to change duration to an int , or handle the case 0 < duration < 1 . 另外,您可能需要将duration更改为int ,或处理0 < duration < 1

Apart from not decrementing the duration (which leads to infinite recursion), it looks like you're not doing anything with the data you get back from your recursive calls. 除了不减少持续时间(这会导致无限递归)外,您似乎还没有对从递归调用中获取的数据进行任何处理。 The key to writing recursive functions is in knowing when to stop, and knowing what to do with the data you get back from each call. 编写递归函数的关键在于知道何时停止,以及如何处理每次调用返回的数据。

In this case, you want to stop when the number of years is 0. If you keep money in the bank for 0 years, you will get no interest, so in the 0 case, you want to return 0. 在这种情况下,您想要在年数为0时停止。如果您将钱存入银行0年,您将不会获得利息,因此在0的情况下,您希望返回0。

For durations greater than 0, since you want to calculate the total interest you accumulate over the years, you should add the interest from the current year with the interest from the remaining years (in which the startUp amount will also include the interest from the current and past years). 对于持续时间大于0的持续时间,由于要计算多年来累积的总利息,因此应将当年的利息与剩余年的利息相加(其中,启动金额还将包括当年的利息)和过去的几年)。 So you'd have something like 所以你会有类似

return (startUp*rate) + calculateInterest(startUp*(1+rate), rate, duration-1);

I have made this program using functions in C 我已经使用C中的函数制作了该程序

//include standard libraries

#include<stdio.h>

//user defined function

float cmp_i(float prin,float rate,int time);

//start of main function

main()
{

        //declaration of variables

        float prin,rate,ci;
        int time;

        //input by the user

        printf("Please enter the principle ammount\n");
        scanf("%f", &prin);
        printf("Please enter thr rate of interest\n");
        scanf("%f",&rate);
        printf("Please enter the time in years");
        scanf("%d", &time);

        //calculation and result display

        ci=cmp_i(prin,rate,time);
        printf("The compound interest for the given ammount is %f",ci);


}//end of main function
//start of cmp_i function


float cmp_i(float prin,float rate,int time)
{

    //declaration of variables
    int i;
    float intr=0;
    intr=(prin*rate)/100;
    for(i=01;i<=time;i++)
    {
        intr=intr+(intr*rate)/100;
    }
    return(intr);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM