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

来源:互联网 发布:小米任我行联通4g知乎 编辑:程序博客网 时间:2024/05/16 01:05
#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){//每一位乘以ifor (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]位,则位数加1if (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;}

0 0
原创粉丝点击