fjnu 1461 Factorials
来源:互联网 发布:酒心巧克力 知乎 编辑:程序博客网 时间:2024/04/29 18:25
Description
The factorial of n, (written as n!) is n * (n - 1) * (n - 2) * ... * 2 * 1. 0! = 1 (go lookup the Gamma function to find out why). In this problem, you'll be calculating the factorial of an integer between 0 and 100. 100! has no more than 200 digits.
Input
There are 1 or more lines of input. Each line has a single integer, N, on it, such that 0 <= N <= 100.
Output
For each line of input, your program should print out N!.
Sample Input
850
Sample Output
4032030414093201713378043612608166064768844377641568960512000000000000
KEY:这题是求大整数的阶乘,我是采用先乘后进位的办法,有点慢就是了;
Source:
#include<iostream>
using namespace std;
void Factorial(int n)
...{
int a[210];
int i,j,k;
for(i=0;i<=200;i++)
...{
a[i]=0;
}
a[1]=1;
k=1;
for(i=2;i<=n;i++)
...{
for(j=1;j<=k;j++)
...{
a[j]=a[j]*i;
}
for(j=1;j<=k;j++)
...{
a[j+1]+=a[j]/10;
a[j]=a[j]%10;
}
while(a[j]>9)
...{
a[j+1]+=a[j]/10;
a[j]=a[j]%10;
if(a[j+1]>0) j++;
}
if(a[j]>0) k=j;
}
for(i=k;i>0;i--)
cout<<a[i];
cout<<endl;
}
int main()
...{
// freopen("fjnu_1185.in","r",stdin);
int n;
while(cin>>n)
...{
Factorial(n);
}
return 0;
}
#include<iostream>
using namespace std;
void Factorial(int n)
...{
int a[210];
int i,j,k;
for(i=0;i<=200;i++)
...{
a[i]=0;
}
a[1]=1;
k=1;
for(i=2;i<=n;i++)
...{
for(j=1;j<=k;j++)
...{
a[j]=a[j]*i;
}
for(j=1;j<=k;j++)
...{
a[j+1]+=a[j]/10;
a[j]=a[j]%10;
}
while(a[j]>9)
...{
a[j+1]+=a[j]/10;
a[j]=a[j]%10;
if(a[j+1]>0) j++;
}
if(a[j]>0) k=j;
}
for(i=k;i>0;i--)
cout<<a[i];
cout<<endl;
}
int main()
...{
// freopen("fjnu_1185.in","r",stdin);
int n;
while(cin>>n)
...{
Factorial(n);
}
return 0;
}
- fjnu 1461 Factorials
- fjnu 1050 Sum of Factorials
- factorials
- Factorials
- Factorials
- fjnu 1461 回文数
- FJNU
- USACO-Factorials
- Small factorials
- USACO:Factorials
- usaco Factorials
- FJNU字符串
- FJNU字符串
- fjnu 1772
- URAL 1083. Factorials!!!
- Sum of Factorials
- USACO 3.2 Factorials
- Sum of Factorials
- SQLite语法 DROP TABLE
- fjnu 1178 Base 9 Calculator
- SQLite语法 DROP INDEX
- SQLite语法 DETACH DATABASE
- SQLite语法 DETACH DATABASE
- fjnu 1461 Factorials
- 数据库中时间的替换
- SQLite语法 DELETE
- fjnu 1291 The Party's in Dolores's Driveway
- SQLite语法 CREATE VIEW
- SQLite语法 CREATE TRIGGER
- SQLite语法 CREATE TABLE
- SQLite语法 CREATE INDEX
- java ftp 客户端(apache jakarta commons net ftp)的bug[原创][Bug]