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

HDU 1042 N!

时间:2017-09-07 00:55:35      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:div   integer   amp   span   tput   tar   file   nbsp   main   

N!

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


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(极光炫影)
 

 

Recommend
We have carefully selected several similar problems for you:  1715 1047 1063 1753 1316 
 
分析:一开始以为要用高精度,然后看了下 其他人的做法,原来可以直接用数组储存当前位
     然后又因为不知道 0!=1  WA了很多发..
     注意,一开始数组需要多长的话,可能需要试探性的取吧,再看ans[MAXN]是否为0
代码如下:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN=40000;
int ans[MAXN];
int flag;
int main()
{
    int n,r,tmp;
while(scanf("%d",&n)!=EOF){
    flag=0;
     memset(ans,0,sizeof(ans));
     ans[0]=1;
     for(int i=2;i<=n;i++)
     {
         r=0;
        for(int j=0;j<=MAXN;j++)
        {
            tmp=ans[j]*i+r;
            ans[j]=tmp%10;
            r=tmp/10;

        }
     }
       for(int i=MAXN;i>=0;i--)
       {
          if(flag==0&&ans[i]==0)
          continue;
          else
          {
              flag=1;
              printf("%d",ans[i]);
          }
       }
       printf("\n");
    }
    return 0;
}

 

 
 
 

HDU 1042 N!

标签:div   integer   amp   span   tput   tar   file   nbsp   main   

原文地址:http://www.cnblogs.com/a249189046/p/7487722.html

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