hdu1042--N!

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1042

想一下10000!一定非常大结果不能一次性输出,所以我们可以采用数组来保存结果;

eg:100!=100*99*98*...*2*1;

我们可以让9900*98...可以转换成大整数乘小整数;

结果假如是:12 3456 7890;

a[0]=7890;a[1]=3456;a[0]=12;

具体过程如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cstdlib>
 7 #include<cstring>
 8 using namespace std;
 9 #define maxn 10010
10
11 int a[maxn];//每个元素里存4位;不然10000的阶乘位数太多会超限;
12
13 void PUL(int a[],int k)
14  {
15     for(int i=0; i<maxn; i++)
16     {
17         a[i]=a[i]*k;
18     }
19     for(int i=0; i<maxn; i++) //进行进位,保证每个元素都含有4位
20     {
21         a[i+1]+=a[i]/10000;
22
23         a[i]%=10000;
24     }
25 }
26
27 void PUT(int a[])
28 {
29     int len;
30
31     for(int i=maxn-1; i>=0; i--)
32      {
33         if(a[i]!=0)
34         {
35             len=i;
36             break;
37         }
38     }
39     printf("%d",a[len]);//末位单独输出;eg:12 0000 0000;
40     for(int i=len-1; i>=0; i--)
41     {
42         printf("%04d",a[i]);
43     }
44     cout<<endl;
45 }
46 int main()
47  {
48     int n;
49
50     while(cin>>n)
51     {
52         memset(a,0,sizeof(a));
53
54         a[0]=1;//0!=1;
55
56         for(int i=1; i<=n; i++)
57         {
58             PUL(a,i);
59         }
60         PUT(a);
61     }
62     return 0;
63 }
时间: 2024-10-24 05:57:09

hdu1042--N!的相关文章

HDU1042(N!)题解

HDU1042(N!)题解 以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 计算N的阶乘并输出. [题目分析] 题给范围上限是10000,那么毫无疑问是大数题.之前我整理过各种大数的代码并且改了改然后所谓封装了一下,于是这道题就直接用了个类似的代码修改了一下,然后...交上去就对了(喂..).但是作为题解,怎么着也得把做法解释清楚吧.. 以下为c语言代码(虽然是cpp提交的,也带了一堆cpp头文件),也附带了代码解释.代码很好懂,看看解释应该看得明白.还有...如果你是写

HDU1042(N!:设4为基数)

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

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

hdu1042

题目大意: 求 n!(可能会超过整数范围,这里用数组模拟n!的值) http://acm.hdu.edu.cn/showproblem.php?pid=1042 AC代码: /** *数组模拟n! */ #include<iostream> #include<cstdio> #include<string.h> using namespace std; int a[40001]; int main() { int n; while(cin>>n){ mems

JAVA大数乘法 HDU1042

1 import java.math.*; 2 import java.util.Scanner; 3 4 5 public class Main{ 6 public static void main(String[] args){ 7 int a; 8 Scanner cin = new Scanner(System.in); 9 while(cin.hasNext()) 10 { 11 BigInteger ans=BigInteger.ONE; 12 a=cin.nextInt(); 13

HDU1042 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 Code: #include <iostream> #incl

hdu1042 N!(大数)

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

杭电HDU1042(有点坑的高精度)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1042 题意: Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 是不是很简单呢? 一般方法: #include<iostream> #include<cstring> using namespace std; const int MAXN = 10000; int a[MAXN]; int main() { in

HDU1042 N! 大数的阶乘

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 由于数字特别大, 所以用数组来模拟. 经测试, 数组大小开50000 可过. 附AC代码, 欢迎大神指点~ #include <cstdio>#include <cstring> const int maxn = 50000 + 100;int a[maxn];void solve(int n){ memset(a, 0, sizeof(a)); a[0] = 1; int p

HDU1042 A * B Problem Plus

A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24620    Accepted Submission(s): 6271 Problem Description Calculate A * B. Input Each line will contain two integers A and B. P