📄 c04b.cpp
字号:
// c04b.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
int main(int argc, char* argv[])
{
// 输入n
int n;
cout << "请输入n:";
cin >> n;
while ( n < 0 )
{
cout << "输入有错,请重输:";
cin >> n;
}
if ( n == 0 )
return 0;
// 求n!的位数
double sum = 1.0;
for ( int i = 1; i <= n; ++i )
sum += log10( i );
int size = (int)sum;
// 分配数组及初始化
char* pa = new char[size];
if (!pa)
{
cout << "too large factor of " << size << endl;
return 0;
}
pa[0] = 1;
for ( i = 1; i < size; ++i )
pa[i] = 0;
// 求 n!
double bitcount = 1;
int begin = 0;
for ( i = 2; i <= n; ++i )
{
long and = 0;
bitcount += log10(i);
if ( pa[begin] == 0 )
begin++;
for ( int j = begin; j < int( bitcount ); j++ )
{
and += i * pa[j];
pa[j] = char( and % 10 );
and /= 10;
}
}
// 打印n!
int bit = 0;
for ( i = size - 1; i >= 0; i-- )
{
if ( bit % 50 == 0 )
cout << endl << " 第"
<< setw(3) << (bit/50+1) << "个50位:";
cout << (int)pa[i];
bit++;
}
cout << endl;
delete[] pa;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -