• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Just Fools.
♕Heart-broken is just a fool.
   首页    新随笔    联系   管理    订阅  订阅

HDU 1042 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
 
题目意思很简单,就是让你求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 }

 

posted @ 2016-02-04 09:10  ♕Heart-broken  阅读(179)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3