阶乘问题(大数阶乘)简单 n! (一个大数与一个小数相乘的算法 、一个大数与一个小数的除法算法 *【模板】 )

sdut oj 简单n!

 

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

给定一个数n(0 <= n <= 150), 求0到n中所有数的阶乘。

输入

题目有多组数据,处理到文件结尾。输入一个数n。

输出

输出阶乘,形式如:4! = 24.每组数据输出后跟一个空行。

示例输入

1

4

示例输出

0! = 1

1! = 1



0! = 1

1! = 1

2! = 2

3! = 6

4! = 24

提示

 代码:
       
#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <algorithm>

#include <iostream>

#include <iomanip>

#include <cctype>

#include <string>



using namespace std;





//实现1->150的阶乘



int main()

{

    int r[601];

    int i, j, k, c;

    int t;

    int n;

    while(scanf("%d", &n)!=EOF)

    {

        if(n==0)

        {

            printf("0! = 1\n\n");

            continue;

        }

        if(n==1)

        {

            printf("0! = 1\n1! = 1\n\n");

            continue;

        }



        printf("0! = 1\n1! = 1\n");

        for(i=0; i<=600; i++)

        {

            r[i]=0;

        }

        

        r[0]=j=1;

        for(i=2; i<=n; i++)

        {

            for(k=0; k<j; k++)

            {

                r[k]=r[k]*i;

            }

            for(k=c=0; k<j; k++ )

            {

                t=r[k]+c;

                r[k]=t%10;

                c=t/10;

            }

            while(c)

            {

                r[j]=c%10;

                c=c/10;

                j++;

            } //处理高位的那部分的进位问题

        //printf("%d---\n", j) ;

            printf("%d! = ", i );

            for(k=j-1; k>=0; k--)

            {

                printf("%d", r[k] );

            }

            printf("\n");

        }

        printf("\n");

    }

   

    return 0;

}

 

除法的没涉及!有待添加





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

    Problem id    : SDUT OJ 2059 

    Result        : Accepted 

    Take Memory    : 496K 

    Take Time    : 10MS 

    Submit Time    : 2015-01-11 08:22:42  

**************************************/
View Code

 

                                               HDU OJ    n!

Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
Input
One N in one line, process to the end of file.
Output
For each N, output N! in one line.
Sample Input
1
2
3
Sample Output
1
2
6
 算法分析:和上一道题目一样也是 大数*小数的数组模拟!
 开二维数组太大开不出来,只好一维每次都要计算一遍。最好是打表存储好1到10000的阶乘数,直接输出就好了,但是10000的阶乘大约有57000位,需要开的内存太大!
 代码:
 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <math.h>

 4 #include <ctype.h>

 5 #include <algorithm>

 6 #define mem(a) memset(a, 0, sizeof(a));

 7 using namespace std;

 8 int r[57000];

 9 int pos[10001];

10 

11 void jiecheng(int n)

12 {

13     int i, j, k, c, t;

14     mem(r);

15     r[0]=j=1;

16     for(i=2; i<=n; i++)

17     {

18         for(k=0; k<j; k++)

19         {

20             r[k]=r[k]*i; //上一行的数值进行 乘计算

21         } //逐位进行计算

22         for(k=c=0; k<j; k++) //初始进位数为0

23         {

24             t=r[k]+c; //当前值+进位数

25             r[k]=t%10; //取余即为该位数

26             c=t/10; //进位数

27         }

28         while(c)

29         {

30             r[j]=c%10;

31             c=c/10;

32             j++;

33         }

34     }

35     for(i=j-1; i>=0; i--)

36         printf("%d", r[i]);

37     printf("\n");

38 }

39 

40 int main()

41 {

42     int n;

43     while(scanf("%d", &n)!=EOF)

44     {

45         if(n==1 ||n==0)

46         {

47             printf("1\n");

48             continue;

49         }

50         else

51         {

52             jiecheng(n);

53         }

54     }

55     return 0;

56 }
View Code

顺便提一下,如何计算一个数n的阶乘结果的位数,这是数学方法,不理解的话记住就好!

所谓n!的十进制位数,就是 log(n)+1, 根据数学公式有:n!=1*2*3*.....*n;

                                         lg(n!)=lg(2)+......lg(n);

代码:

#include <string>

#include <iostream>

#include <iomanip>

#include <stdio.h>

#include <cmath>

 

using namespace std;

 

int main()

{

    long int n;

    long int i;

    double sum;

 

    while(scanf("%ld", &n)!=EOF)

    {

        sum=0.0;

        for(i=2; i<=n; i++)

        {

            sum+=log10(i);

        }

        printf("%ld\n", (int)sum+1 );

    }

    return 0;

}
View Code

 

 

 

你可能感兴趣的:(算法)