hdu 1042 N!

N!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 83969    Accepted Submission(s): 24758


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
 

 

Author
JGShining(极光炫影)
 
 
题解:也是一道大整数和整数的乘法     可以使用java来做
java对于大整数的题目  相比C,C++来说    有很大的优势    毕竟它自带了大整数0.0
以下是代码:
特别注意以下  java  一定要定义成Main
 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3  
 4  
 5 public class Main {
 6 
 7     public static void main(String[] args) {
 8         Scanner cin = new Scanner ( System.in );
 9         BigInteger one = BigInteger.ONE;
10         while ( cin.hasNext() ) {
11             BigInteger N = cin.nextBigInteger();
12             BigInteger sum = new BigInteger ( "1" );
13             for ( BigInteger i = BigInteger.ONE; i.compareTo(N) <= 0; i = i.add(one) ) {
14                 sum = sum.multiply( i );
15             }
16             System.out.println( sum );
17         }
18     }
19  
20 }
View Code

也可以使用C,C++来敲的

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 using namespace std;
 5 int a[100002];
 6 int main()
 7 {
 8     int n;
 9     int i,j,k,count,temp;
10     while(cin>>n)
11     {
12         a[0]=1;
13         count=1;
14         for(i=1;i<=n;i++)
15         {
16             k=0;
17             for(j=0;j<count;j++)
18             {
19                 temp=a[j]*i+k;
20                 a[j]=temp%10;
21                 k=temp/10;
22             }
23             while(k)//记录进位
24              {
25                 a[count++]=k%10;
26                 k/=10;
27             }
28         }
29         for(j=100001;j>=0;j--)
30             if(a[j])
31                 break;//忽略前导0
32             for(i=count-1;i>=0;i--)
33                 cout<<a[i];
34             cout<<endl;
35     }
36     return 0;
37 }
View Code

 

posted @ 2017-09-06 09:52  红雨520  阅读(159)  评论(0编辑  收藏  举报