# joj 1386解题报告

## 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
`

#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;
}

• 本文已收录于以下专栏：

## URAL 1386 Maze 解题报告

• u010638776
• 2013年07月07日 16:23
• 790

## UVA - 1386 Cellular Automaton

• u011345136
• 2014年09月01日 20:46
• 1067

## JOJ 1069: Joseph 解题报告

• xiewei20082008
• 2009年07月27日 14:32
• 232

## joj 1019解题报告

1019: Do the UntwistResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE3s8192K992556StandardCryptog...
• neoxuhaotian
• 2011年01月24日 19:06
• 656

## JOJ 1073: Piggy-Bank 解题报告

• xiewei20082008
• 2009年07月29日 09:53
• 250

## JOJ 1068: Cipher 解题报告

• xiewei20082008
• 2009年07月26日 20:05
• 253

## JOJ 1054: Area 解题报告

• xiewei20082008
• 2009年07月23日 10:19
• 156

## Windows中，文件所在路径查找命令

c:/workspace/TestProject>dir/a/s *.java 查看当前目录下以及子目录下所有的java文件所在的路径。...
• sxzlc
• 2013年04月30日 14:45
• 671

## db2codepage=1386

• mydriverc2
• 2014年08月08日 19:42
• 962

## JOJ 1055: Cog-Wheels 解题报告

• xiewei20082008
• 2009年07月23日 22:23
• 443

举报原因： 您举报文章：joj 1386解题报告 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)