求大数阶乘存储在数组中

#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;
}

相关文章

自1998年我国取消了福利分房的政策后,房地产市场迅速开展蓬...
文章目录获取数据查看数据结构获取数据下载数据可以直接通过...
网上商城系统MySql数据库设计
26个来源的气象数据获取代码
在进入21世纪以来,中国电信业告别了20世纪最后阶段的高速发...