HDU 1042 N! (大数阶乘,还是Java大法好,C也不能错过!!!)

N!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 73270    Accepted Submission(s): 21210

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

Author

JGShining(极光炫影)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042

大数!还是Java大发好,刘汝佳紫书上有一道阶乘的例题,但是超时!!!具体原因看代码!

AC代码1(Java)

import java.math.BigDecimal;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			System.out.println(fun(n));
		}
	}

	public static BigDecimal fun(int n) {
		BigDecimal s = new BigDecimal(1);
		for (int i = 1; i <= n; i++) {
			BigDecimal a = new BigDecimal(i);
			s = s.multiply(a);
		}
		return s;
	}

}

AC代码2(C模拟)

#include<stdio.h>
#include<string.h>
#define maxn 50000
int f[maxn];
int main()
{
    int i,j,n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(f,0,sizeof(f));
        f[0]=1;
        int count=1;
        for(i=1; i<=n; i++)
        {
            int c=0;//进位
            for(j=0; j<count; j++)//阶乘位数
            {
                int s=f[j]*i+c;
                f[j]=s%10;
                c=s/10;
            }
            while(c)
            {
                f[count++]=c%10;
                c/=10;
            }
        }
        for(j=maxn-1; j>=0; j--)
            if(f[j]) break;
        for(i=j; i>=0; i--)
            printf("%d",f[i]);
        printf("\n");
    }
    return 0;
}

超时代码:

#include<stdio.h>
#include<string.h>
#define maxn 50000
int f[maxn];
int main()
{
    int i,j,n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(f,0,sizeof(f));
        f[0]=1;
        for(i=2; i<=n; i++)
        {
            int c=0;
            for(j=0; j<maxn; j++)//长度,每次都到最大,导致超时!!!!
            {
                int s=f[j]*i+c;
                f[j]=s%10;
                c=s/10;
            }
        }
        for(j=maxn-1; j>=0; j--)
            if(f[j]) break;
        for(i=j; i>=0; i--)
            printf("%d",f[i]);
        printf("\n");
    }
    return 0;
}
时间: 2024-07-30 03:53:07

HDU 1042 N! (大数阶乘,还是Java大法好,C也不能错过!!!)的相关文章

hdu 1042 N!(大数阶乘,转化为100000这样的比较大的进制)

N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 54172    Accepted Submission(s): 15365 Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one

HDU 1042 N!(大数阶乘)

N! 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 代码: 1 #include<cstdio> 2 usi

POJ 1053 Integer Inquiry (大数加法,还是Java大法好)

Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32674   Accepted: 12789 Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he

HDU-1042-N!(Java大法好 &amp;&amp; HDU大数水题)

N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 64256    Accepted Submission(s): 18286 Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in o

HDU 1042 大数阶乘

B - 2 Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1042 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. Outpu

HDU 4873 ZCC Loves Intersection(JAVA、大数、推公式)

在一个D维空间,只有整点,点的每个维度的值是0~n-1 .现每秒生成D条线段,第i条线段与第i维度的轴平行.问D条线段的相交期望. 生成线段[a1,a2]的方法(假设该线段为第i条,即与第i维度的轴平行)为,i!=j时,a1[j]=a2[j],且随机取区间[0,n-1]内的整数.然后a1[i],a2[i]在保证a1[i]<a2[i]的前提下同样随机. 由于D条线段各自跟自己维度的轴平行,我们可以转换成只求第i个维度与第j个维度的相交期望,然后乘以C(2,n)就好了 显然线段[a1,a2]和线段[

HDU 1316 How Many Fibs?(java,简单题,大数)

题目 /** * compareTo:根据该数值是小于.等于.或大于 val 返回 -1.0 或 1: public int compareTo(BigInteger val) 将此 BigInteger 与指定的 BigInteger 进行比较. 对于针对六个布尔比较运算符 (<, ==, >, >=, !=, <=)中的每一个运算符的各个方法,优先提供此方法. 执行这些比较的建议语句是:(x.compareTo(y) <op> 0), 其中 <op> 是

HDU 1250 Hat&#39;s Fibonacci(Java大数相加)+讲解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n -

51 Nod 1057 N的阶乘【Java大数乱搞】

1057 N的阶乘 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 输入N求N的阶乘的准确值. Input 输入N(1 <= N <= 10000) Output 输出N的阶乘 Input示例 5 Output示例 120 题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1057 分析:学了简单的Java,就来体验了一波Java的爽感,Java大法真的好啊! 下面给出AC代码: