一本通-1172:求10000以内n的阶乘

【题目描述】

求10000以内n的阶乘。

【输入】

只有一行输入,整数n(0<=n<=10000)。

【输出】

一行,即n!的值。

【输入样例】

4

【输出样例】

24

10000*10000 用int 可以存储的下,思路和大整数加法类似,边计算边进位,否则最后两个样例会超时

http://ybt.ssoier.cn:8088/problem_show.php?pid=1172

#include <iostream>
using namespace std;
#define coutInt(a,b)  cout << "#" << a << "==" << b << endl;
#define coutArr(a,len) for(int i = 1; i <= len; i++) cout << a[i] << " "; cout << endl;

int n;
const int maxn = 1e5 + 11;
int a[maxn];

int main(int argc, char const *argv[]){

	cin >> n;
	if(n == 0 || n == 1){
		cout << 1;
		return 0;
	}
	memset(a,0,sizeof(a));
	a[0] = 1; //代表长度
	a[1] = 1; //从下表1,开始存储数据
	for(int i = 2; i <= n; i++){
		int temp = 0;
		for(int j = 1; j <= a[0]; j++){
			a[j] *= i;
			a[j] += temp;
			if(a[j] > 9){
				temp = a[j]/10;
				a[j] = a[j]%10;
				if(j == a[0]) a[j+1] = temp;
			}else{
				temp = 0;
			}

		}
		//处理末尾一位的进位
		int k = a[0] + 1;
		while(a[k] > 9){
			a[k+1] += a[k]/10;
			a[k] = a[k]%10;
			k++;
		}
		//处理数组长度
		if(a[k] > 0) a[0] = k;
		else a[0] = k-1;
		//coutArr(a,a[0]);
	}
	//输出
	for(int i = a[0]; i >= 1; i--) cout << a[i];
	
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值