题目大意: 输入一个正整数n,输出n!的值。
其中n!=1*2*3*…*n。
解题思路: 高精度加法及乘法,使用数组模拟存储计算结果。
//蓝桥杯试题 基础练习 阶乘计算
//高精度
//Writed by Maolin Xiao,2020 03 08
//妇女节快乐!!!
#include<bits/stdc++.h>
#define fori(i,a,b) for(int i = a;i < b;i++)
#define mod 1000000007
#define ll long long
#define pi acos(-1)
#define ford(i,a,b) for(int i = a;i >= b;i--)
#define fast_input() ios::sync_with_stdio(0)
#define INF 0x3f3f3f3f
#define maxn 10005
using namespace std;
//使用倒叙存储数字,方便于加法的实现
typedef struct BigNumber{
int len;
int value[maxn];
void init(){
len = 0;
memset(value,0,sizeof(value));
}
}BigNumber;
BigNumber Add(BigNumber a,BigNumber b){
BigNumber ans;
ans.init();
int flag = 0;
fori(i,0,max(a.len,b.len)){
int q = a.value[i] + b.value[i] + flag;
if(q >= 10) {
flag = 1;
q %= 10;
}
else{
flag = 0;
}
ans.value[ans.len++] = q;
}
if(flag) ans.value[ans.len++] = flag;
return ans;
}
void Print(BigNumber it){
ford(i,it.len - 1,0){
printf("%d",it.value[i]);
}
cout<<endl;
}
BigNumber mulity(BigNumber a,int t){
BigNumber ans;
ans.init();
while(t){
int k = t % 10;
fori(i,0,k){
ans = Add(ans,a);
}
t /= 10;
a.len++; //a扩大十倍
ford(i,a.len - 1,1){
a.value[i] = a.value[i - 1];
}
a.value[0] = 0;
}
return ans;
}
int main(){
int n;
cin>>n;
BigNumber a;
a.init();
a.len = 1;
a.value[0] = 1; //a初始化为1
fori(i,2,n + 1){
a = mulity(a,i);
}
Print(a);
return 0;
}
如有问题,欢迎联系2339814485(qq).