大数阶乘

来源:互联网 发布:三国乱世盘古辅助淘宝 编辑:程序博客网 时间:2024/06/08 20:03
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点。//大数阶乘N http://blog.csdn.net/petershina/article/details/45070359//http://blog.csdn.net/abcjennifer/article/details/7271844//http://blog.csdn.net/liuchang54/article/details/40379833#include "stdafx.h"#include<string>#include<iostream>using namespace std;int a[10000];int main(){int n; int len = 1; a[1] = 1;//用len模拟乘积的位数,初始为1位cin >> n;for (int i = 1; i <= n; i++)//1-n  n个数字{for (int j = 1; j <= len; j++)a[j] = a[j] * i;//逐位相乘----a[j]代表第j位for (int j = 1; j <= len; j++){if (a[j] < 10) continue;//结束当前循环-break是结束全部循环int pos = j;while (pos <= len){if (a[len] > 9) len++;//如果当前位大于9.进位a[pos + 1] = a[pos] / 10 + a[pos + 1];a[pos] = a[pos] % 10;pos++;}}}for (int i = len; i >= 1; i--){cout << a[i];}}

原创粉丝点击