UVa 10220 - I Love Big Numbers!

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

中文網址

Solution

由下往上做每個階乘。
在算每個大數時,邊記下總位數,方便處理進位。

Code

UVa 10220
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<cstdio>

int main()
{
static int factorial[1001][5000] = { { 0 }, { 1 } };
int i, j, idx = 0, ans[1001] = { 0, 1 };

for (i = 2; i < 1001; i++)
{
for (j = 0; j <= idx; j++)
factorial[i][j] = factorial[i - 1][j] * i;

for (j = 0; j <= idx; j++)
{
if (factorial[i][j] >= 10)
{
int temp = factorial[i][j] / 10;
factorial[i][j] %= 10;
factorial[i][j + 1] += temp;

if (j == idx)//記目前總位數
idx++;
}

ans[i] += factorial[i][j];
}
}

int n;
while (scanf("%d", &n) != EOF)
printf("%d\n", ans[n]);

return 0;
}