zoukankan      html  css  js  c++  java
  • N! http://acm.hdu.edu.cn/showproblem.php?pid=1042

    N!

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


    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
     
    Author
    JGShining(极光炫影)
    #include <stdio.h>
    #include<string.h>
    #define M 40000 int main() { int i,n,j; int a[M+10]; while(scanf("%d",&n)!=EOF) { memset(a,0,sizeof(a)); a[0]=1; for(i=1;i<=n;i++) { for(j=0;j<M;j++) { a[j]=a[j]*i; } for(j=0;j<M;j++) { if(a[j]>=10) { a[j+1]+=a[j]/10; a[j]%=10; } } } for(i=M;(a[i]==0)&&(i>=0);i--); if(i>=0) for(;i>=0;i--) printf("%d",a[i]); else printf("0"); printf(" "); } return 0; }

    做这道题,需要用大数相乘的知识,当时比赛,我没用心想大数相乘以至于当时没做出来。今天用了几分钟就搞定了。其实它的原理很简单就是把大数存放在数组中,然后对每一位乘i,乘完之后再考虑进位,然后去0,倒叙输出,有个地方要注意,就是n的范围,可不敢在while里边写n<=10000作为循环条件,因为那样会超时。

  • 相关阅读:
    wait waitpid
    达梦备份还原
    sigprocmask阻塞信号
    sigaction信号处理
    dd命令
    linux系统启动过程
    cpio建立、还原备份档
    configure详解
    Git入门基础教程
    一篇文章了解Github和Git教程
  • 原文地址:https://www.cnblogs.com/wangyouxuan/p/3240038.html
Copyright © 2011-2022 走看看