24h購物| | PChome| 登入
2012-03-15 21:49:54| 人氣2,272| 回應0 | 上一篇 | 下一篇

[UVA] 623 - 500!

推薦 0 收藏 0 轉貼0 訂閱站台

  500! 

In these days you can more and more often happen to see programs which perform some useful calculations being executed rather then trivial screen savers. Some of them check the system message queue and in case of finding it empty (for examples somebody is editing a file and stays idle for some time) execute its own algorithm.

As an examples we can give programs which calculate primary numbers.


One can also imagine a program which calculates a factorial of given numbers. In this case it is the time complexity of order O(n) which makes troubles, but the memory requirements. Considering the fact that 500! gives 1135-digit number no standard, neither integer nor floating, data type is applicable here.


Your task is to write a programs which calculates a factorial of a given number.

Assumptions: Value of a number ``n" which factorial should be calculated of does not exceed 1000 (although 500! is the name of the problem, 500! is a small limit).

Input 

Any number of lines, each containing value ``n" for which you should provide value of n!

Output 

2 lines for each input case. First should contain value ``n" followed by character `!'. The second should contain calculated value n!.

Sample Input 

10
30
50
100

Sample Output 

10!
3628800
30!
265252859812191058636308480000000
50!
30414093201713378043612608166064768844377641568960512000000000000
100!
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000



#include <stdio.h>
#include <stdlib.h>
int Ans[1001][3000] = {};
int main() {
int i, j, last = 0, n;
Ans[0][0] = 1;
for(i = 1; i <= 1000; i++) {
for(j = 0; j <= last; j++)
Ans[i][j] = Ans[i-1][j]*i;
for(j = 0; j <= last; j++) {
Ans[i][j+1] += Ans[i][j]/10;
Ans[i][j] %= 10;
}
while(Ans[i][j] != 0) {
Ans[i][j+1] += Ans[i][j]/10;
Ans[i][j] %= 10;
j++;
}
last = j;
}
while(scanf("%d", &n) == 1) {
i = 2999;
printf("%d!\n", n);
while(Ans[n][i] == 0) i--;
for(; i >= 0; i--)
printf("%d", Ans[n][i]);
puts("");
}
return 0;
}
 


台長: Morris
人氣(2,272) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 10127 - Ones
此分類上一篇:[UVA] 838 - Worm World

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文