28-大数阶乘

题目描述:

我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?

输入描述:

输入一个整数m(0 
  

输出描述:

输出m的阶乘,并在输出结束之后输入一个换行符

样例输入:

50

样例输出:

30414093201713378043612608166064768844377641568960512000000000000

大数,Java   AC代码:



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

public class Main {
	public static void main(String[] args) {
		BigInteger  result = new BigInteger("1");
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()) {
			int base = sc.nextInt();
			 for(int i = 1; i <= base; i++){ 
				 String  temp1 = Integer.toString(i); 
				 BigInteger temp2 = new BigInteger(temp1);
			 result = result.multiply(temp2); } 
			 System.out.println(result.toString());
		}
	}
}

 

 

c++ ,AC代码:

#include 
#include 
using namespace std;
const int maxn = 200000;
int a[maxn];
int main(){
    int n;
    ios::sync_with_stdio(false);
    while(cin >> n){
      memset(a,0,sizeof(a));
      a[0] = 1;
      int temp = 0;
      for(int i=1;i<=n;i++){
            a[0] = a[0]*i;
           for(int j=1;j<=temp;j++){
              a[j]=a[j]*i + (a[j-1]/10);
              a[j-1] = a[j-1]%10;
           }
           while(a[temp]>10){
              a[temp+1] = a[temp]/10;
              a[temp] = a[temp]%10;
              temp++;
           }
      }
      for(int i=temp;i>=0;i--)
        cout << a[i];
      cout << endl;
    }
    return 0;
}

 

你可能感兴趣的:(Java,SE,NYOJ,大数)