C#递归(乘法) [英] C# recursion (multiplication)

查看:125
本文介绍了C#递归(乘法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个值,知道如何计算,但是我不知道如何放入递归代码.
任何建议请...谢谢您
金额$ 10000
利率10%
投资期10年
总价值为25美元,937.42

主要通话Ryear(10)返回23579.47
n = 10返回23579.47 * 1.1
n = 9返回21435.89 * 1.1
n = 8返回19487.17 * 1.1
n = 7返回17715.61 * 1.1
n = 6返回16105.1 * 1.1
n = 5返回14641 * 1.1
n = 4返回13310 * 1.1
n = 3返回12100 * 1.1
n = 2返回11000 * 1.1
n = 1返回10000 * 1.1
n = 0返回10000

这只是我从课堂上得到的例子...

i have this value and know how to calculate it but i don''t know how to put in a recursion code.
Any advice pls...Thank you
the amount $10000
the interest rate 10%
the investment period 10 years
the total value would $25, 937.42

main calls Ryear(10) return 23579.47
n = 10 return 23579.47 * 1.1
n = 9 return 21435.89 * 1.1
n = 8 return 19487.17 * 1.1
n = 7 return 17715.61 * 1.1
n = 6 return 16105.1 * 1.1
n = 5 return 14641 * 1.1
n = 4 return 13310 * 1.1
n = 3 return 12100 * 1.1
n = 2 return 11000 * 1.1
n = 1 return 10000 * 1.1
n = 0 Return 10000

this just is the example i got from my class...

static void Main(string[] args)
{
    Console.WriteLine(Ryear(10));
    Console.ReadLine();
}
static private int Ryear(int n)
{
      if (n < 1)
             return 0;
      else
      return Ryear(n-1)+ n;

}

推荐答案

10000
利率10%
投资期10年
总值将
10000
the interest rate 10%
the investment period 10 years
the total value would


25,937.42

主要通话Ryear(10)返回23579.47
n = 10返回23579.47 * 1.1
n = 9返回21435.89 * 1.1
n = 8返回19487.17 * 1.1
n = 7返回17715.61 * 1.1
n = 6返回16105.1 * 1.1
n = 5返回14641 * 1.1
n = 4返回13310 * 1.1
n = 3返回12100 * 1.1
n = 2返回11000 * 1.1
n = 1返回10000 * 1.1
n = 0返回10000

这只是我从课堂上得到的例子...
25, 937.42

main calls Ryear(10) return 23579.47
n = 10 return 23579.47 * 1.1
n = 9 return 21435.89 * 1.1
n = 8 return 19487.17 * 1.1
n = 7 return 17715.61 * 1.1
n = 6 return 16105.1 * 1.1
n = 5 return 14641 * 1.1
n = 4 return 13310 * 1.1
n = 3 return 12100 * 1.1
n = 2 return 11000 * 1.1
n = 1 return 10000 * 1.1
n = 0 Return 10000

this just is the example i got from my class...
static void Main(string[] args)
{
    Console.WriteLine(Ryear(10));
    Console.ReadLine();
}
static private int Ryear(int n)
{
      if (n < 1)
             return 0;
      else
      return Ryear(n-1)+ n;

}


这(复合兴趣 http://en.wikipedia.org/wiki/Simple_interest#Simple_interest [ ^ ])是滥用递归的经典示例.在您的方法中,递归的深度等于循环数(在您的示例中为年).

使用索引范围为0到N-1年而不是递归的常规for循环.

至于您尝试使用递归,不看实现就很容易看到:您的递归函数只有一个参数,有多个循环,并且没有定义百分比,因此从原理上讲是不正确的. >
就是这样:我指出了您的第一个错误,并解释了替代方法.那应该足够公平.现在轮到你了.

—SA
This (compounded interest, http://en.wikipedia.org/wiki/Simple_interest#Simple_interest[^]) is a classical example of misuse of recursion. In your approach, the depth of recursion is equal to the number of cycles (years in your example).

Use a regular for loop with index running 0 to N-1 years, not recursion.

As to you attempt to use recursion, it''s easy to see without looking at the implementation: your recursive function has only one parameter, a number of cycles, and nothing defines percentage, so it cannot be correct in principle.

That''s it: I pointed out your first mistake and explained alternative approach. That should be fair enough. Now it''s your turn.

—SA


这篇关于C#递归(乘法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆