链接:https://www.nowcoder.com/acm/contest/75/E
来源:牛客网
时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
给定一个整数N(0≤N≤10000),求取N的阶乘
输入描述:
多个测试数据,每个测试数据输入一个数N
输出描述:
每组用一行输出N的阶乘
示例1
输入
1
2
3
输出
1
2
来源:牛客网
时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
给定一个整数N(0≤N≤10000),求取N的阶乘
输入描述:
多个测试数据,每个测试数据输入一个数N
输出描述:
每组用一行输出N的阶乘
示例1
输入
1
2
3
输出
1
2
6
题意:大数阶乘。
思路:用数组存储,数字顺序倒过来存放,方便处理进位。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int a[100010], n, cnt, tmp, jinw;
int main()
{
IO;
while(cin >> n){
a[0] = 1;cnt=1;jinw=0;
for(int i = 1; i <= n; i++){
for(int j = 0; j < cnt; j++){
tmp = a[j]*i+jinw;
a[j] = tmp%10;//只存最末位
jinw = tmp/10;//可能是多位,因为i可能是多位
}
while(jinw){
a[cnt++]=jinw%10;
jinw=jinw/10;
}
}
for(int i = cnt-1; i >= 0; i--){
cout << a[i];
}
cout << endl;
}
return 0;
}