<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>
            C語言

            如何使用C語言求N的階乘

            時間:2025-03-23 13:03:37 C語言 我要投稿
            • 相關推薦

            如何使用C語言求N的階乘

              使用C語言求N的階乘的方法是很多小伙伴都想知道的,下面小編給大家介紹如何使用C語言求N的階乘,歡迎閱讀!

              用遞歸法求N的階乘

              程序調用自身稱為遞歸( recursion).它通常把一個大型復雜的問題層層轉化為一個與原問題相似的規模較小的問題來求解.

              遞歸的能力在于用有限的語句來定義對象的無限集合。

              一般來說,遞歸需要有邊界條件、遞歸前進段和遞歸返回段。當邊界條件不滿足時,遞歸前進;當邊界條件滿足時,遞歸返回。

            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            36
            37
            38
            39
            40
            41
            42
            43
            #include <stdio.h>
            #include <string.h>
            #include <stdlib.h>
            long factorial(int n)
            {
             if(n == 1)
               return 1;
             else
               return n*factorial(n-1);
            }
            int main(int argc,char *argv[])
            {
             int n = 0;
             if(argc != 2)
             {
               printf("input error,exit!! ");
               return -1;
             }
             n = atoi(argv[1]);
             printf("%d! = %ld ",n,factorial(n));
             return 0;
            }

              習題示例

              題目

              題目描述:

              輸入一個正整數N,輸出N的階乘。

              輸入:

              正整數N(0<=N<=1000)

              輸出:

              輸入可能包括多組數據,對于每一組輸入數據,輸出N的階乘

              樣例輸入:

              4

              5

              15

              樣例輸出:

              24

              120

              1307674368000

              AC代碼

            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            36
            37
            38
            39
            40
            41
            42
            43
            44
            45
            46
            47
            48
            49
            50
            51
            52
            #include <stdio.h>
            #include <stdlib.h>
            #include <string.h>
             
            #define MAX 3000
             
            //存儲每次階乘運算的結果
            int str[MAX];
             
            void calculateFactorial(int n);
             
            int main()
            {
             int n;
             
             while (scanf("%d", &n) != EOF) {
               if(n == 0) {
                 printf("1 ");
               else {
                 calculateFactorial(n);
               }
             }
             
             return 0;
            }
             
            void calculateFactorial(int n)
            {
             int i, j, temp, c, len;
             
             memset(str, 0, sizeof(str));
             str[1] = 1;
             
             for (i = 2, len = 1; i <= n; i ++) { //循環與2,3,..n相乘
               for (j = 1, c = 0; j <= len; j ++) { //str數組代表一個數,模擬與i相乘
                 temp = str[j] * i + c;
                 str[j] = temp % 10;
                 c = temp / 10;
               }
               while(c > 0)
               {
                 str[j ++] = c % 10;
                 c /= 10;
               }
               len = j - 1;
             }
             
             for (i = len; i >= 1; i --) {
               printf("%d", str[i]);
             }
             printf(" ");
            }

              /**************************************************************

              Problem: 1076

              User: wangzhengyi

              Language: C

              Result: Accepted

              Time:2150 ms

              Memory:916 kb


            【如何使用C語言求N的階乘】相關文章:

            C語言EOF如何使用05-01

            如何使用C語言數組指針04-27

            C語言中如何使用sscanf04-12

            如何使用C語言開發DSP系統12-12

            C語言中如何使用隨機數02-18

            C語言的內存使用03-06

            C語言變量的使用03-30

            C語言typedef的使用05-30

            C語言for循環的使用04-05

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看