中文字幕 另类精品,亚洲欧美一区二区蜜桃,日本在线精品视频免费,孩交精品乱子片免费

<sup id="3hn2b"></sup>

    1. <sub id="3hn2b"><ol id="3hn2b"></ol></sub><legend id="3hn2b"></legend>

      1. <xmp id="3hn2b"></xmp>

      2. 新聞中心

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

        實(shí)例講解|徹底弄懂C語(yǔ)言遞歸

        作者: 時(shí)間:2025-03-11 來(lái)源: 收藏

        1. 漢諾塔:

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

        請(qǐng)輸入盤(pán)子數(shù),輸出盤(pán)子移動(dòng)的操作步驟。

        #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. 爬樓梯:

        樹(shù)老師爬樓梯,他可以每次走1級(jí)或者2級(jí),輸入樓梯的級(jí)數(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. 爬樓梯:

        樹(shù)老師爬樓梯,他可以每次走1級(jí)、2級(jí)或者3級(jí),輸入樓梯的級(jí)數(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ù)列:

        請(qǐng)輸入項(xiàng)數(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. 求階乘:

        請(qǐng)輸入整數(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. 取球問(wèn)題:

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

        #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個(gè)人坐在一起,問(wèn)第5個(gè)人多少歲,他說(shuō)比第4個(gè)人大2歲。問(wèn)第4個(gè)人多少歲,他說(shuō)比第3個(gè)人大2歲。問(wèn)第3個(gè)人多少歲,他說(shuō)比第2個(gè)人大2歲。問(wèn)第2個(gè)人多少歲,他說(shuō)比第1個(gè)人大2歲。最后問(wèn)第1個(gè)人,他說(shuō)是10歲。請(qǐng)問(wèn)第5個(gè)人多大?

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

        9. 猴子吃桃問(wèn)題:

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

        #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. 猴子吃桃問(wèn)題:

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

        #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ù):

        利用算法求兩個(gè)數(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. 逆序輸出:

        輸入一個(gè)正整數(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. 逆序輸出:

        輸入一個(gè)字符串,將該字符串逆序輸出。

        #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語(yǔ)言 遞歸

        評(píng)論


        相關(guān)推薦

        技術(shù)專(zhuān)區(qū)

        關(guān)閉