加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

HDU 1042 N! N的阶乘(大数)

发布时间:2021-01-26 05:28:04 所属栏目:大数据 来源:网络整理
导读:N! Time Limit: 10000/5000 MS (Java/Others)????Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 75303????Accepted Submission(s): 21981 Problem Description Given an integer N(0 ≤ N ≤ 10000),your task is to calculate N! ? In

N!

Time Limit: 10000/5000 MS (Java/Others)????Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 75303????Accepted Submission(s): 21981


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
求n的阶乘嘛! 基础题,依然WA,也是够了!
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<queue>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f

#define N 1100000
int a[N];

int main()
{
    int i,j,n,count,t,k;
    while(scanf("%d",&n)!=EOF)
    {
        a[0]=1;
        count=1;
        for(i=1;i<=n;i++)
        {
            k=0;
            for(j=0;j<count;j++)
            {
                t=a[j]*i+k;
                a[j]=t%10;
                k=t/10;
            }
            while(k)
            {
                a[count++]=k%10;
                k=k/10;
            }
        }
        for(i=count-1;i>=0;i--)
        {
            printf("%d",a[i]);
        }
        printf("n");
    }
    return 0;
}


10000的阶乘也就35660位,要是担心TLE,或者超限的话,也可以按10000进制来写:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<queue>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f

#define N 1100000
int a[N];

int main()
{
    int i,&n)!=EOF)
    {
        a[0]=1;
        count=1;
        for(i=1;i<=n;i++)
        {
            k=0;
            for(j=0;j<count;j++)
            {
                t=a[j]*i+k;
                a[j]=t%10000;
                k=t/10000;
            }
            while(k)
            {
                a[count++]=k%10000;  ///这里以10000为进位单位
                k=k/10000;
            }
        }
        printf("%d",a[count-1]);  ///注意一下,第一个数不是“%04d”;
        for(i=count-2;i>=0;i--)
        {
            printf("%04d",a[i]);  ///其他的按四位的输出,不足补零
        }
        printf("n");
    }
    return 0;
}

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读