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

HDOJ 1042 N! 大数

发布时间:2021-01-12 02:02:11 所属栏目:大数据 来源:网络整理
导读: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 123 Sample Output 126 思路:没啥思路

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

思路:没啥思路啊。。就大数搞,用大数模拟乘法,从低位到高位每一位都和i相乘,然后处理进位;
最后除去首0元素逆序输出即可;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 40000// 10000的阶乘最少36000位
using namespace std;
int a[N],n;
int main()
{ ? ?int i,j,tem,s;
? ? ?while(cin>>n)
? ? ?{ ? ?
? ? ? ? ? memset(a,sizeof(a));//每次初始化?
 ? ? ?a[0]=1;
? ? ? ? ? for(i=2;i<=n;i++)
? ? ? ? ? { ? tem=0;
? ? ? ? ? for(j=0;j<N;j++)
? ? ? ? ? { ? ?s=a[j]*i+tem;
? ? ? ? ? ? ? ?a[j]=s%10;
? ? ? ? ? ? ? ?tem=s/10;
? ? ? ? ? }
? ? ? ? ? }
? ? ? ? ? i=N-1;
? ? ? ? ? while(i>=0&&a[i]==0)
? ? ? ? ? i--;
? ? ? ? ? for(i;i>=0;i--)
? ? ? ? ? printf("%d",a[i]);
? ? ? ? ? printf("n");
? ? ? ? ??
? ? ?}
? ? ?return 0;
}

(编辑:核心网)

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

    热点阅读