高中生程式解題系統:c120: 00623–500!

Ping-Lun Liao
2 min readNov 4, 2019

--

題目連結 https://zerojudge.tw/ShowProblem?problemid=c120
用陣列來處理,此部分可參考 超長整數運算(大數運算)一文。

程式碼:

#include <iostream>
#include <cmath>
using namespace std;int main(void){
const int MAX_SIZE = 9999;
int i , k;
double j;
int *p = new int[MAX_SIZE];
int num;
int size;
while( cin >> num )
{
j = 0;
for (i = 1 ; i <= num ; i++){
j += log10(i*1.0);
}
size = (int)j ; j = 0;
p[0] = 1;
for (i = 1 ; i <= size ; i++)
p[i] = 0;
for (i = 2 ; i <= num ; i++){
for ( k = 0 ; k < j + 1 ; k++){
p[k] *= i;
}
j += log10(i*1.0);
for ( k = 0 ; k < j + 1 ; k++){
p[ k + 1 ] += p[ k] / 10;
p[k] %= 10;
}
} cout << num << "!" << endl;
for (i = size ; i >= 0 ; i-- )
cout << p[i];
cout << endl; } return 0;
}

Original: https://yunlinsong.blogspot.com/2019/11/c120-00623-500.html

--

--

No responses yet