高精度练习(hdoj1042)
Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
Input
One N in one line, process to the end of file.
Output
For each N, output N! in one line.
Sample Input
1
2
3
Sample Output
1
2
6
#include <stdio.h> #include <stdlib.h> char* myblog[] = { "http://www.cnblogs.com/archimedes/", "hdoj1042", "mail: codingwu@gmail.com"}; int a[50000]; void count(int n) { int i, flag, digit, j, t; a[0] = 1; digit = 1; j = 1; for(i = 2; i <= n; i++) { flag = 0; for(j = 0; j < digit; j++) { t = a[j] * i + flag; if(t >= 10) { a[j] = t % 10; flag = t / 10; } else { a[j] = t; flag = 0; } } if(flag) { while(flag) { a[j] = flag % 10; flag /= 10; digit++; j++; } } } for(i = j - 1; i >= 0; i--) printf("%d", a[i]); printf("\n"); } void solve() { int n; while(scanf("%d", &n) != EOF) { if(n == 0) printf("1\n"); else count(n); } } int main() { solve(); return 0; }
作者:wuyudong
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
如果觉得本文对您有帮助,可以对作者进行小额【赞助】