微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

求大数阶乘存储在数组中

#include "stdafx.h"
//求N!  0<=N<=1000  
#include <iostream>
using namespace std;

#include <vector>
int main()
{
	vector<int> res(10000,0);
	int n;
	while (cin >> n)
	{
		//第0位标记位数
		res[0] = 1;
		res[1] = 1;
		for (int i = 2; i <= n; ++i)
		{
			//每一位乘以i
			for (int j = 1; j <= res[0]; ++j)
				res[j] *= i;
			//调整每一位使得每一位为一位数
			for (int j = 1; j <= res[0]; ++j)
			{
				if (res[j] >= 10)
				{
					res[j + 1] += (res[j] / 10);
					res[j] %= 10;
					//如果超出了res[0]位,则位数加1
					if (j == res[0])
						++res[0];
				}
			}
		}
		//输出
		for (int i = res[0]; i > 0; --i)
			cout << res[i];
		cout << endl;
		res.clear();
		res.assign(10000,0);
	}
	return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐