求N!

来源:互联网 发布:常用的下载软件的端口 编辑:程序博客网 时间:2024/04/30 07:17

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.

#include<iostream>
using namespace std;
int main()
{
 int result[40000];
 int num;
 while(cin>>num)
 {
  int height=1;  //结果的最高位
  if(num>10000||num<0) break;
  result[0]=1;
  for(int i=1;i<=num;i++)
  {
   int res=0;  //进位
   for(int j=0;j<height;j++)
   {
    int buf=result[j]*i+res;  //计算结果
    result[j]=buf;   //取当前位
    res=buf/10;   //计算进位
   }
   while(res)
   {
    result[height++]=res;  //取当前位
    res/=10;  //计算进位
   }
  }
  for(int k=height-1;k>=0;k--)
   cout<<result[k];
  cout<<endl;
 }
 return 0;
}


0 0
原创粉丝点击