码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1042 N! 高精度乘法

时间:2016-02-04 09:42:38      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

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
 
题目意思很简单,就是让你求N的阶乘,但是N的范围有10000,所以要用高精度,但是10000的时候枚举乘上去时间不够,还好数据没有10000的。
我的代码应该是可以优化的。
代码如下:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 using namespace std;
 6 int temp,n,a,k,ans[110000];
 7 int main(){
 8     while(scanf("%d",&n)!=EOF)
 9     {
10         int count=1;
11     memset(ans,0,sizeof(ans));
12         ans[0]=1;
13     
14         for (int i=1;i<=n;++i)
15         {    k=0;
16             for(int j=0;j<count;++j)
17             {
18                temp=ans[j]*i+k;
19                ans[j]=temp%10;
20                k=temp/10;
21                
22                        
23             }while(k){
24                    ans[count++]=k%10;
25                    k/=10;
26                }
27         }
28         for (int i=100000;i>=0;--i)
29         if (ans[i])
30         {
31             count=i;
32             break;
33         }
34         for(int i=count;i>=1;--i)
35         {
36             printf("%d",ans[i]);
37         }
38         printf("%d\n",ans[0]);
39     }
40     
41     return 0;
42 }

 

HDU 1042 N! 高精度乘法

标签:

原文地址:http://www.cnblogs.com/fakerv587/p/5180943.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!