• 周三. 4月 24th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

C#——获取阶乘(递归、循环)

admin

11月 28, 2021
一个正整数的阶乘factorial)是所有小于及等于该数的正整数的积,并且0的阶乘为1。自然数n的阶乘写作n!。1808年,基斯顿·卡曼引进这个表示法。

亦即n!=1×2×3×…×(n-1)×n。阶乘亦可以递归方式定义:0!=1,n!=(n-1)!×n

根据定义,用递归的方式获取阶乘。

 1 /// <summary>
 2 /// 递归获取阶乘
 3 /// </summary>
 4 /// <param name="i">乘数</param>
 5 /// <param name="max">阶乘</param>
 6 /// <param name="result">当前结果</param>
 7 /// <returns></returns>
 8 public int nFactorial(int i, int max, int result) {
 9     if (i <= max) {
10         result *= i;
11         result = nFactorial(i + 1, max, result);
12     }
13     return result;
14 }

当然,我们更习惯直接用循环获取阶乘。

 1 /// <summary>
 2 /// 循环获取阶乘
 3 /// </summary>
 4 /// <param name="n"></param>
 5 /// <returns></returns>
 6 public int nFactorial_while(int n) {
 7     int result = 1;
 8     if (n <= 0) {
 9         return 1;
10     }
11     for (int i = 1; i <= n; i++) {
12         result *= i;
13     }
14     return result;
15 }
有志者,事竟成,破釜沉舟,百二秦关终属楚; 苦心人,天不负,卧薪尝胆,三千越甲可吞吴。

《C#——获取阶乘(递归、循环)》有一个想法
  1. PBN sites
    We’ll create a structure of privately-owned blog network sites!

    Pros of our PBN network:

    We perform everything SO THAT Google doesn’t realize that this is A PBN network!!!

    1- We obtain web domains from different registrars

    2- The leading site is hosted on a virtual private server (VPS is fast hosting)

    3- Other sites are on distinct hostings

    4- We attribute a individual Google ID to each site with confirmation in Google Search Console.

    5- We design websites on WP, we don’t use plugins with assistance from which malware penetrate and through which pages on your websites are established.

    6- We never reiterate templates and utilise only unique text and pictures

    We never work with website design; the client, if wished, can then edit the websites to suit his wishes

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注