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,所以要用高精度,但是10000的时候枚举乘上去时间不够,还好数据没有10000的。

我的代码应该是可以优化的。

代码如下:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 using namespace std;
 6 int temp,n,a,k,ans[110000];
 7 int main(){
 8     while(scanf("%d",&n)!=EOF)
 9     {
10         int count=1;
11     memset(ans,0,sizeof(ans));
12         ans[0]=1;
13
14         for (int i=1;i<=n;++i)
15         {    k=0;
16             for(int j=0;j<count;++j)
17             {
18                temp=ans[j]*i+k;
19                ans[j]=temp%10;
20                k=temp/10;
21
22
23             }while(k){
24                    ans[count++]=k%10;
25                    k/=10;
26                }
27         }
28         for (int i=100000;i>=0;--i)
29         if (ans[i])
30         {
31             count=i;
32             break;
33         }
34         for(int i=count;i>=1;--i)
35         {
36             printf("%d",ans[i]);
37         }
38         printf("%d\n",ans[0]);
39     }
40
41     return 0;
42 }
时间: 2024-08-06 15:42:45

HDU 1042 N! 高精度乘法的相关文章

Hdu 1042 N! (高精度数)

Problem Description Givenan integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input OneN in one line, process to the end of file. Output Foreach N, output N! in one line. Sample Input 1 2 3 Sample Output 1 2 6 Author JGShining(极光炫影) /********* 高

HDU1402 FFT高精度乘法模板题

#include<bits/stdc++.h> using namespace std; //HDU 1402 求高精度乘法 const double PI = acos(-1.0); //复数结构体 struct Complex { double x,y;//实部和虚部x+yi Complex(double _x = 0.0,double _y = 0.0) { x = _x; y = _y; } Complex operator -(const Complex &b)const {

HDU 1042.N!【高精度乘法】【8月24】

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 高精度乘法.数组存,每一位存5位数,不然会严重超时.另外,

hdu 1063 Exponentiation (高精度小数乘法)

//大数继续,额,要吐了. Problem Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. This problem re

高精度乘法程序

对于超过20位的数的乘法问题,我们无法使用普通的方法!!!即使是longlong也会超出范围的!像这样的数,我们只能使用高精度的知识利用数组的方法解决问题!对于高精度乘法的问题,其实思路和高精度加法的思路差不多,都需要使用字符数组来存放每次算完的结果!        1  2  3        *4  5  6    ________________      12  15  18   8  10  124  5   6  _____________________4 13   28   27

POJ 1306 Combinations 高精度乘法

题目大意:给出mn,让你求C(m,n). 思路:公式都给你了,就100,暴力就可以关键还是高精度.如果按照算法"它让你怎么做你就怎么做",那么很显然你需要写一个高精度除法.然而可以证明,这个除法是不会产生余数的.所以我们可以数论分析,然后避免高精度除法. 方法就是暴力求每个数的质因数,然后把被除数和除数相同的质因数消去,最后除数肯定会被消没.这样只要做高精度乘法就可以了. CODE: #include <cstdio> #include <cstring> #i

poj1001(高精度乘法)

1.题目表述 Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 135893   Accepted: 33256 Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation o

真真真&#183;?高精度乘法!!!!!

RX-0奉上哈哈哈哈哈哈哈哈哈哈哈哈哈™™™ 先奉上真真真·高精度乘法源代码: 高精度乘法 RX-0制作最后修改:2016年7月6日#include<stdio.h>#include<string.h>#include<math.h>char s[10000],b;int a[10000];int c[10000];int main(){ int x,l=0,y=1,i,j,m,l1=0; long long s1=0; //freopen("hp.in&qu

[转]高精度乘法计算

转载自: Daywei 高精度乘法计算 高精度乘法计算基础 1.高精度浮点运算方法 高精度浮点(Floating Point,FP)运算可以转换成整数型运算.由于高精度浮点数可以看成是由整数部分(Integer Part,IP)与小数部分(Decimal Part,DP)的组合,因此其乘法可以看成以下3种运算的组合,即整数x整数(IxI).整数x小数(IxD)和小数x小数(DxD).用表达式表示, 则FP1*FP2=IP1*IP2+(IP1*DP2+IP2*DP1)+DP1*DP2 (1)对于I