高精度练习(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;
}
目录
相关文章
|
11月前
hdoj 1166 敌兵布阵
暴力超时,这道题可以用线段树做,因为更新的是单个节点,我们也可以用数组数组来做,我将两种方法的代码都给出 数组数组最适宜的用途就是区间求和和点的更新,但树状数组并不适用于区间的更新问题,也不是做不到,比较麻烦且难理解,有兴趣的可以看看这个
24 0
|
3月前
|
C++
【洛谷 P1601】A+B Problem(高精)题解(高精度+向量)
该问题要求解决高精度加法(正数)的A+B问题。给定两个不超过10^500的大整数a和b,程序需输出它们的和。样例输入包括两个整数,如1和1,输出为2;另一样例是1001和9099,输出为10100。解决方案通过模拟十进制加法实现,代码使用C++,将输入转换为字符数组,然后逐位相加并处理进位。最终结果反向输出。
23 0
High-precision Depth Estimation with the 3D LiDAR and Stereo Fusion(使用3D雷达和立体融合的高精度深度估计)思维导图
High-precision Depth Estimation with the 3D LiDAR and Stereo Fusion(使用3D雷达和立体融合的高精度深度估计)思维导图
114 0
High-precision Depth Estimation with the 3D LiDAR and Stereo Fusion(使用3D雷达和立体融合的高精度深度估计)思维导图
HDOJ 2073 无限的路
HDOJ 2073 无限的路
101 0
HDOJ 2073 无限的路
|
算法 Java
HDOJ 2117 Just a Numble(模拟除法)
HDOJ 2117 Just a Numble(模拟除法)
82 0
HDOJ 2054 A == B ?(精确大数相等)
HDOJ 2054 A == B ?(精确大数相等)
120 0
HDOJ 2200 Eddy's AC难题(数学组合概率题)
HDOJ 2200 Eddy's AC难题(数学组合概率题)
102 0
|
知识图谱
HDOJ(HDU) 1985 Conversions(汇率转换)
HDOJ(HDU) 1985 Conversions(汇率转换)
108 0
HDOJ 2080 夹角有多大II
HDOJ 2080 夹角有多大II
90 0
|
Java
HDOJ 1753 大明A+B(大数~)
HDOJ 1753 大明A+B(大数~)
104 0