日本a√视频在线,久久青青亚洲国产,亚洲一区欧美二区,免费g片在线观看网站

        <style id="k3y6c"><u id="k3y6c"></u></style>
        <s id="k3y6c"></s>
        <mark id="k3y6c"></mark>
          
          

          <mark id="k3y6c"></mark>

          新聞中心

          EEPW首頁 > 設(shè)計應(yīng)用 > 實例講解|徹底弄懂C語言遞歸

          實例講解|徹底弄懂C語言遞歸

          作者: 時間:2025-03-11 來源: 收藏

          1. 漢諾塔:

          本文引用地址:http://yuyingmama.com.cn/article/202503/467918.htm

          請輸入盤子數(shù),輸出盤子移動的操作步驟。

          #include
          void move(char fromchar to) {
             printf("%c to %cn"fromto);
          }
          void hanoi(int nchar achar bchar c) {
             if (n == 1)
                 move(ac);
             else {
                 hanoi(n - 1acb);
                 move(ac);
                 hanoi(n - 1bac);
            }
          }
          void main() {
             int n;
             scanf("%d"&n);
             hanoi(n'A''B''C');
          }

          2. 爬樓梯:

          樹老師爬樓梯,他可以每次走1級或者2級,輸入樓梯的級數(shù),求不同的走法數(shù)。

          #include
          intstair(intn) {
          if (n==1return1;
          if (n==2return2;
          returnstair(n-1+stair(n-2);
          }
          voidmain() {
          intn;
          scanf("%d"&n);
          printf("%d"stair(n));
          }

          3. 爬樓梯:

          樹老師爬樓梯,他可以每次走1級、2級或者3級,輸入樓梯的級數(shù),求不同的走法數(shù)。

          #include
          intstair(intn) {
          if (n==1return1;
          if (n==2return2;
          if (n==3return4;
          returnstair(n-1+stair(n-2+stair(n-3);
          }
          voidmain() {
          intn;
          scanf("%d"&n);
             printf("%d"stair(n));
          }

          4. 斐波那契數(shù)列:

          請輸入項數(shù),輸出具體數(shù)列。

          #include
          int fibonacci(int n) {
             if (n == 1 || n == 2)
                 return 1;
             return fibonacci(n - 1+ fibonacci(n - 2);
          }
          void main() {
             int ni;
             scanf("%d"&n);
             for (i = 1i <= ni++)
                 printf("%d,"fibonacci(i));
          }

          5. 求階乘:

          請輸入整數(shù)n,求1!+2!+3!+4!+5!+6!+7!+…+n!的和。

          #include
          int factorial(int n) {
             if (n == 1return 1;
             return n * factorial(n - 1);
          }
          void main() {
             int nisum = 0;
             scanf("%d"&n);
             for (i = 1i <= ni++)
                 sum += factorial(i);
             printf("sum=%d"sum);
          }

          6. 取球問題:

          在n個球中,任意取m個(不放回),求有多少種不同取法。

          #include
          int ball(int nint m) {
             if (n < m)  return 0;
             if (n == mreturn 1;
             if (m == 0return 1;
             return ball(n - 1m - 1+ ball(n - 1m);
          }
          void main() {
             int nm;
             scanf("%d%d"&n&m);
             printf("%d"ball(nm));
          }

          7. 楊輝三角:

          輸入要打印的層數(shù),打印楊輝三角。

          #include
          int triangle(int mint n) {
             if (m == 0 || n == 0 || m == n)
                 return 1;
             return triangle(m - 1n+ triangle(m - 1n - 1);
          }
          void main() {
             int nij;
             scanf("%d"&n);
             for (i = 0i < ni++) {
                 for (j = 0j <= ij++) {
                     printf("%d "triangle(ij));
                }
                 printf("n");
            }
          }

          8. 求年齡:

          有5個人坐在一起,問第5個人多少歲,他說比第4個人大2歲。問第4個人多少歲,他說比第3個人大2歲。問第3個人多少歲,他說比第2個人大2歲。問第2個人多少歲,他說比第1個人大2歲。最后問第1個人,他說是10歲。請問第5個人多大?

          #include
          int age(int n) {
             if (n == 1return 10;
             return age(n - 1+ 2;
          }
          void main() {
             printf("%d"age(5));
          }

          9. 猴子吃桃問題:

          猴子第一天摘下若干個桃子,當(dāng)即吃了一半,還不癮,又多吃了一個。第二天早上又將剩下的桃子吃掉一半,又多吃了一個。以后每天早上都吃了前一天剩下的一半多一個。到第十天早上想再吃時,見只剩下一個桃子了。問最初有多少個桃子。

          #include
          int peach(int n) {
             if (n == 10return 1;
             return (peach(n + 1+ 1* 2;
          }
          void main() {
             printf("%d"peach(1));
          }

          循環(huán):

          #include
          void main() {
             int is = 1;
             for (i = 9i >= 1i--) {
                 s = (s + 1* 2;
            }
             printf("%d"s);
          }

          10. 猴子吃桃問題:

          猴子第一天摘下若干個桃子,當(dāng)即吃了一半,還不癮,又多吃了一個。第二天早上又將剩下的桃子吃掉一半,又多吃了一個。以后每天早上都吃了前一天剩下的一半多一個。第十天同樣是吃了前一天的一半加一個,最后剩下一個桃子。問最初有多少個桃子。

          #include
          int peach(int n) {
             if (n == 11return 1;
             return (peach(n + 1+ 1* 2;
          }
          void main() {
             printf("%d"peach(1));
          }

          循環(huán):

          #include
          void main() {
             int is = 1;
             for (i = 10i >= 1i--) {
                 s = (s + 1* 2;
            }
             printf("%d"s);
          }

          11. 最大公約數(shù):

          利用算法求兩個數(shù)的最大公約數(shù)。

          #include
          /* 最大公約數(shù) */
          int gcd(int aint b) {
             int t;
             if (a < b) {
                 t = a;
                 a = b;
                 b = t;
            }
             if (b == 0) {
                 return a;
            }
             return gcd(ba % b);
          }
          void main() {
             int ab;
             scanf("%d%d"&a&b);
             printf("gcd=%d"gcd(ab));
          }

          12. 逆序輸出:

          輸入一個正整數(shù),將該正整數(shù)逆序輸出。

          #include
          void printDigit(int n) {
             printf("%d"n % 10);
             if (n > 10) {
                 printDigit(n / 10);
            }
          }
          void main() {
             int n;
             scanf("%d"&n);
             printDigit(n);
          }

          13. 逆序輸出:

          輸入一個字符串,將該字符串逆序輸出。

          #include
          void printStr(char *str) {
             if (*str != '?')
                 printStr(str + 1);
             if (*str != '?')
                 printf("%c"*str);
          }
          void main() {
             char str[100];
             gets(str);
             printStr(str);
          }


          關(guān)鍵詞: C語言 遞歸

          評論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉