hdu 1042 N!

来源:互联网 发布:java能做什么游戏 编辑:程序博客网 时间:2024/06/05 17:15

N!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 53746    Accepted Submission(s): 15203


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
123
 

Sample Output
126

求n的阶乘。

模拟乘法运算,很精辟的算法!!

#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 50000int p[maxn];int main(){    int n;    while(cin >> n)    {        memset(p,0,sizeof(p));        p[0] = 1;        for(int i=2;i<=n;i++)        {            int c = 0;            for(int j=0;j<maxn;j++)            {                int x = p[j]*i + c;                p[j] = x%10;                c  = x/10;            }        }        int s = maxn-1;        while(p[s]==0) s--;        for(int i=s;i>=0;i--)        {            printf("%d",p[i]);        }        printf("\n");    }    return 0;}


0 0
原创粉丝点击