加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

求大数阶乘(存储在数组中)

发布时间:2020-12-14 03:02:38 所属栏目:大数据 来源:网络整理
导读:#include "stdafx.h"//求N! 0=N=1000 #include iostreamusing namespace std;#include vectorint main(){vectorint res(10000,0);int n;while (cin n){//第0位标记位数res[0] = 1;res[1] = 1;for (int i = 2; i = n; ++i){//每一位乘以ifor (int j = 1; j =
#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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读