http://acm.hdu.edu.cn/showproblem.php?pid=1042Problem DescriptionGiven an integer N(0 ≤ N ≤ 10000), your task is to calculate N! InputOne N in one line, process to the end of file. OutputFor each N, output N! in one line. Sample Input123 Sample Output126 题意描述:输入一个数 N(0 ≤ N ≤ 10000) 计算并输出该数的阶乘值解题思路:模拟小学生乘法计算过程为:用上一次阶乘结果的每一位与阶数相乘,期间每一位与阶数相乘后加上之前的进位等于 t,t 对 10 取余更改当前位,进位数等于去掉各位数字的数例如 23456*33t=33*6+0=198,该位存 8,jw=19,t=33 与下一位 5 直接相乘 165 加上进位 19 程序代码:
- 1#include 2 const int N = 50000;
- 3 int main() 4 {
- 5 int n,
- i,
- j,
- k,
- jw,
- t,
- count,
- a[11];
- 6 int result[N];
- 7
- while (scanf("%d", &n) != EOF) 8 {
- 9 result[0] = 1;
- 10
- for (k = 1, i = 1; i <= n; i++) 11 {
- 12
- for (jw = 0, j = 0; j) 13 {
- 14 t = result[j] * i + jw;
- 15 result[j] = t % 10;
- 16 jw = t / 10;
- 17
- } //直到上一个阶乘结果处理完后,将结果数组扩大,存进进位数即可
- 18
- while (jw) 19 {
- 20 result[++k - 1] = jw % 10;
- 21 jw /= 10;
- 22
- }
- 23
- }
- 24
- for (i = k - 1; i >= 0; i--) 25 printf("%d", result[i]);
- 26 printf("\n");
- 27
- }
- 28
- return 0;
- 29
- }
HDU 1042 N!
来源: http://www.bubuko.com/infodetail-2080073.html