// 100!.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <iomanip>
using namespace std;
vector<int> g_iResult;
void Factorial(int n)
{
if (g_iResult.size() == 0)
{
g_iResult.push_back(1);
}
if (n == 1 || n == 0)
{
return;
}
else
{
int *element = g_iResult.data();
for (int i = 0; i < g_iResult.size(); i ++)
{
*element = *element * n;
element++;
}
if (g_iResult.back() > 1000)
{
int small = g_iResult.back() % 1000;
int big = g_iResult.back() / 1000;
g_iResult.pop_back();
g_iResult.push_back(small);
g_iResult.push_back(big);
}
element = g_iResult.data();
for (int i = 0; i < g_iResult.size(); i ++)
{
if (*element > 1000)
{
int big = *element / 1000;
*element = *element % 1000;
element ++;
*element = *element + big;
}
else
{
element ++;
}
}
Factorial( n - 1 );
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Factorial(1000);//求20的阶乘
cout<<g_iResult.back();
for (int i = g_iResult.size() - 2; i >= 0; i--)
{
printf("%03d", g_iResult.at(i));
//cout<<setfill('0')<<setw(3)<<g_iResult.at(i);
}
return 0;
}
C++_阶乘
最新推荐文章于 2024-03-29 16:25:52 发布