题目传送门: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1057
题意:题目就是要求n的阶乘,但n <= 100000,显然要用到乘法模拟
大致思路就是将一个数分段储存起来,由于此题n最大是100000,所以就按5位数字存在一个元素当中。每次乘的时候就用当前数组中元素从低位到高位分别去乘,该进位的进位就行了。
代码如下:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <string>
#define ll long long
using namespace std;
const int maxn = 10005;
const int mod = 100000;
int main()
{
int a[maxn];//用来存大数
int n,len = 1;
memset(a,0,sizeof(a));
a[0] = 1;
cin >> n;// 要阶乘的数n
int c = 0;
int i,j;
for(i=2;i<=n;i++){//从2开始乘直到n
c = 0;
for(j=0;j<len;j++){//让a数组中存的数分段跟i相乘
a[j] = (c += a[j] * i) % mod; // c 用来判断是否需要进位 ,将a[j]跟i相乘后膜_max存起来
c /= mod;
}
if((a[j] = c) > 0) len++;//如果c/mod后还 >0 说明需要进位
}
printf("%d",a[--len]);
while(len >= 1) printf("%05d",a[--len]);//输出时按一个元素五位输出
return 0;
}