UVA623-500!(模擬乘法 or BigInteger)

題目鏈接

In these days you can more and more often happen to see programs which perform some useful calculations being executed rather then trivial screen savers. Some of them check the system message queue and in case of finding

 it empty (for examples somebody is editing a file and stays idle for some time) execute its own algorithm.

As an examples we can give programs which calculate primary numbers.

One can also imagine a program which calculates a factorial of given numbers. In this case it is the time complexity of order O(n) which makes troubles, but the memory requirements. Considering the fact that 500! gives 1135-digit number no

 standard, neither integer nor floating, data type is applicable here.

Your task is to write a programs which calculates a factorial of a given number.

Assumptions: Value of a number ``n" which factorial should be calculated of does not exceed 1000 (although 500! is the name of the problem, 500! is a small limit).

Input

Any number of lines, each containing value ``n" for which you should provide value of n!

Output

2 lines for each input case. First should contain value ``n" followed by character `!'.

 The second should contain calculated value n!.

Sample Input

10

30

50

100

Sample Output

10!

3628800

30!

265252859812191058636308480000000

50!

30414093201713378043612608166064768844377641568960512000000000000

100!

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000


題目大意

求n!,n的範圍是1-1000,只需要輸出即可。

思路

1.用數組存這個大數,模擬乘法的進位。下標0是低位,比如f[6][2],f[6][1],f[6][0]分別是720,即6!=720。

2.或者用Java的BigInteger類,不用加包名,類名寫成Main

代碼參考:https://blog.csdn.net/synapse7/article/details/11859307

BigInteger的簡單使用:https://blog.csdn.net/lsk1998/article/details/83057662


C++:

#include <stdio.h>

using namespace std;

int f[1010][3010];

void init()
{
    f[0][0] = 1;
    f[1][0] = 1;
    for(int i=2;i<=1001;i++){
        for(int j=0;j<=3000;j++){
            f[i][j] += f[i-1][j] * i;
            f[i][j+1] += f[i][j]/10;
            f[i][j] = f[i][j]%10;
        }
    }
}

int main(){
    init();
    int n;
    while(~scanf("%d",&n))
    {
        printf("%d!\n",n);
        int m=3000;
        while(f[n][m]==0) m--;
        for(int i=m;i>=0;i--){
            printf("%d",f[n][i]);
        }printf("\n");
    }
    return 0;
}

Java:

import java.math.BigInteger;
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {  
        Scanner cin=new Scanner(System.in);
        while (cin.hasNext())
        {
        	BigInteger num=new BigInteger("1");
        	BigInteger flat=new BigInteger("1");
        	int n=cin.nextInt();
        	for (int i = 0; i < n; i++) {
            flat=flat.multiply(num);
            num=num.add(new BigInteger("1"));
        	}
        	System.out.println(n+"!\n"+flat);
        }
    }
}

 

發佈了62 篇原創文章 · 獲贊 29 · 訪問量 4753
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章