N!---hdu-1042

N!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 65262    Accepted Submission(s):
18665

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<string.h>
#define max 36000
int main()
{
    int a[max],i,j,n,k,p;
    while(scanf("%d",&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        a[1]=1;
        int c;
        for(i=2;i<=n;i++)
        {
            c = 0;
            for(j=1;j<max;j++)
            {
                a[j] = a[j] * i + c;
                c = a[j] / 10;
                a[j] = a[j] % 10;
            }
        }
        for(i=max-1;(a[i]==0)&&(i>=0);i--);
        if(i>=0)
        {
            for(;i>=1;i--)
            printf("%d",a[i]);
        }
        printf("\n");
        }
    return 0;
}

这题的思路就是2*3*4*5*...n。把所乘得的数存放在数组中,记得进位!!!

再放个我最开始的代码,哎,超时的伤,你不懂!

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define max 36000
 4 int main()
 5 {
 6     int a[max],i,j,n,k,p;
 7     while(scanf("%d",&n),n>=0)
 8     {
 9
10         memset(a,0,sizeof(a));
11         a[1]=1;
12         for(i=2;i<=n;i++)
13         {
14             for(k=1;k<max;k++)
15             a[k]*=i;
16             for(j=1;j<max;j++)
17             {
18                 if(a[j]>=10)
19                 {
20                 a[j+1]+=a[j]/10;
21                 a[j]%=10;
22                 }
23             }
24         }
25         for(i=max-1;(a[i]==0)&&(i>=0);i--);
26         if(i>=0)
27         {
28             for(;i>=1;i--)
29             printf("%d",a[i]);
30         }
31         printf("\n");
32
33     }
34     return 0;
35 }
时间: 2024-10-11 11:51:25

N!---hdu-1042的相关文章

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 大数阶乘

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 1042 N! 参考代码

请问DIY题目做不来的队员:听进去了吗?去消化吸收了吗?能百度一下吗? 请问集训队员:有兴趣吗?有团队合作精神吗?有责任感吗?能坚持吗?能自主学习吗?能承受挫败吗? HDU 1042 N! 题意:Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! (题目链接) #include <iostream> using namespace std; //每个数组元素存放5位数 const int MAX=1000000; //

HDU 1042 N!

题目来源: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

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 o

HDU - 1042 - N! - JAVA

http://acm.hdu.edu.cn/showproblem.php?pid=1042 大数嘛,直接用JAVA. 为什么要开64路?好像我觉得会快一点--其实并没有快-- import java.io.*; import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while

hdu 1042

高精度大数运算其实就是由于数字过大必须要用数组来存数据 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1020 #include<stdio.h> #include<math.h> #include<iterator> #include<iostream> #include<algorithm> #include<set> using namespace std; int main()

HDU 1042 大数计算

这道题一开始就采用将一万个解的表打好的话,虽然时间效率比较高,但是内存占用太大,就MLE 这里写好大数后,每次输入一个n,然后再老老实实一个个求阶层就好 java代码: 1 /** 2 * @(#)Main.java 3 * 4 * 5 * @author 6 * @version 1.00 2014/12/21 7 */ 8 import java.util.*; 9 import java.math.*; 10 11 public class Main { 12 //public stati

HDU 1042 N!

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

HDU 1042 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 题目意思很简单,就是让你求N的阶乘,但是N的范围有10000,所