joj 1386解题报告

原创 2011年01月21日 21:20:00

 1386: 500!


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s 8192K 2898 518 Standard

In these days you can more and more often happen to see programs which perform some useful calculations being executed rather then trivial screen savers. Some of them check the system message queue and in case of finding it empty (for examples somebody is editing a file and stays idle for some time) execute its own algorithm.

As an examples we can give programs which calculate primary numbers.

 


One can also imagine a program which calculates a factorial of given numbers. In this case it is the time complexity of order O(n) which makes troubles, but the memory requirements. Considering the fact that 500! gives 1135-digit number no standard, neither integer nor floating, data type is applicable here.

 


Your task is to write a programs which calculates a factorial of a given number.

Assumptions: Value of a number "n" which factorial should be calculated of does not exceed 500.

Input

Any number of lines, each containing value "n" for which you should provide value of n!

Output

2 lines for each input case. First should contain value "n" followed by character `!'. The second should contain calculated value n!. Mind that visually big numbers will be automatically broken after 80 characters.

Sample Input

 

10
30
50
100

Sample Output

10!
3628800
30!
265252859812191058636308480000000
50!
30414093201713378043612608166064768844377641568960512000000000000
100!
93326215443944152681699238856266700490715968264381621468592963895217599993229915
608941463976156518286253697920827223758251185210916864000000000000000000000000
这道题就是高精度阶乘,将结果用数组保存,然后模拟正常笔算,注意输出格式,每当输出了80个字符时,若还有字符,先换行,在输出。
代码:
语言:c++
#include<iostream>
using namespace std;
int main()
{
int n,a[10000],len,k;
int factorial(int a[],int n);//将结果保存在数组a中,返回结果的长度-1
while(cin>>n)
{
cout<<n<<'!'<<endl;
len=factorial(a,n);
k=1;
for(i=len;i>=0;--i,++k)//k为计数器
{
cout<<a[i];
if(k%80==0&&i)//每当输出了80个数字且i不等于0是,输出换行
cout<<endl;
}
cout<<endl;
}
return 0;
}
int factorial(int a[],int n)
{
int temp,high,carry,i,j;
memset(a,0,sizeof(a));//将数组a所有元素全部置为0
a[0]=1;
if(n==0)
return 0;
else
{
high=0;//high为结果当前的最高位,开始时为0
for(i=1;i<=n;++i)
{
carry=0;//carry为进位
for(j=0;j<=high;j++)//模拟笔算,a[0]为数字的最低位
{
temp=a[j]*i+carry;
a[j]=temp%10;
carry=temp/10;
}
if(carry)//将进位的数全部除尽
{
while(carry)
{
a[j++]=carry%10;
carry/=10;
high=j-1;//确定结果当前的最高位数
}
}
}
return high;
}

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

joj 1386解题报告

1386: 500!ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE3s8192K2898518StandardIn these days you ...

POJ 3150 / Uva 1386 Cellular Automaton 解题报告(循环矩阵)

题目大意:细胞上的数字会随着周围数字的和而变化,求第k轮之后数字的情况。     解题报告:很容易可以想到矩阵乘法。用快速幂的确可以很快的计算出k次方来。     可是n最大有500,矩阵乘法的复杂度...
  • kbdwo
  • kbdwo
  • 2013-08-13 10:40
  • 832

joj 1920解题报告

1920: JojerResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE3s8192K733231StandardMr Jojer is a teac...

joj 1031 Hanoi Tower Troubles Again!解题报告

1031: Hanoi Tower Troubles Again!ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE3s8192K1034606Sta...

joj 1019解题报告

1019: Do the UntwistResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE3s8192K992556StandardCryptogra...

浙大 ACM 解题报告

  • 2015-12-30 21:51
  • 835KB
  • 下载

BZOJ 3680 爬山算法 解题报告

3680: 吊打XXXDescriptiongty又虐了一场比赛,被虐的蒟蒻们决定吊打gty。gty见大势不好机智的分出了n个分身,但还是被人多势众的蒟蒻抓住了。蒟蒻们将 n个gty吊在n根绳子上,...

HDU 2000-2099 解题报告.CHM

  • 2013-08-08 20:51
  • 831KB
  • 下载

HDU2501解题报告

  • 2014-07-10 10:44
  • 12KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)