高精度练习(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;
}

 

posted @ 2014-04-19 00:04  wuyudong  阅读(293)  评论(0编辑  收藏  举报
Top_arrow