问题描述:
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.
问题分析:这道题典型的是一道大数模板的应用。建立两个数组a[i],b[j],数组的每一个元素保存一个四位数,升位用一个变量up表示。结果先表示为一个临时变量,最后保存到res[i+j]数组中。
计算公式如下:
temp= a[i]*b[j]+res[i+j]+up;
AC代码:
// N! #include<iostream> using namespace std; #include <cstring> #define N 10003 int main() { ios::sync_with_stdio(false); int n; int a[N]; //len1表示位数 int i,j,up,len1; while(cin>>n){ len1 = 0; a[0] = 1; for(i = 1;i <= n;i++){ up = 0; for(j = 0;j <= len1;j++){ a[j] = a[j]*i+up; up = a[j] / 10000; a[j] %= 10000; } if(up > 0){ len1++; a[len1] = up; } } cout<<a[len1]; for(i = len1-1;i >= 0;i--){ cout.width(4); cout.fill('0'); cout<<a[i]; } cout<<endl; } return 0; }