📄 c04a.cpp
字号:
// c04a.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
#include <stdlib.h>
int getN();
int getBitNum( int n );
char * init( int size );
void calc( char* a, int n );
void display( char* s, int size );
int main(int argc, char* argv[])
{
int n = getN();
int size = getBitNum( n );
char * pa = init( size );
calc( pa, n );
display( pa, size );
delete[] pa;
return 0;
}
int getN()
{
int n;
cout <<"请输入n!中的n:";
cin >> n;
while( n<0 )
{
cout << "输入有错,请重输:";
cin >> n;
}
if ( n==0 )
exit(1);
return n;
}
int getBitNum( int n )
{
double sum = 1.0;
for ( int i = 1; i <= n; ++i )
sum += log10( i );
return (int)sum;
}
char* init( int size )
{
char* pa = new char[size];
if (!pa)
{
cout <<"too large factor of "<<size<<endl;
exit(1);
}
pa[0] = 1;
for ( int i = 1; i < size; ++i )
pa[i] = 0;
return pa;
}
void calc( char* a, int n )
{
double bitcount = 1;
int begin = 0;
for ( int i = 2; i <= n; ++i )
{
long and = 0;
bitcount += log10(i);
if ( a[begin] == 0 )
begin++;
for ( int j = begin; j < int(bitcount); j++ )
{
and += i * a[j];
a[j] = char( and % 10 );
and /= 10;
}
}
}
void display( char* a, int size )
{
int bit = 0;
for ( int i = size - 1; i >= 0; i-- )
{
if ( bit % 50 == 0 )
cout << endl << "第" << setw(3) << (bit/50+1) << "个50位:";
cout << (int)a[i];
//cout << a[i]; // DON'T DO THIS, THE RESULT IS SEVERE!
bit++;
}
cout << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -