- 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\
题意:
就是求n的阶乘。
思路:
开一个数组模拟乘法运算即可。
#include<iostream> #include<cstdio> #include<string> #include<cmath> #include<cstring> using namespace std; const int mx = 41000; int a[mx]; int main (){ int n; while(scanf("%d",&n) != EOF){ memset(a, 0, sizeof(a)); a[0] = a[1]= 1; for(int i=2 ;i <= n; i++){ int c = 0; for(int j = 1; j < mx; j++){ int te = a[j] * i + c; a[j] = te % 10; c = te / 10; } } int tt; for(int i = mx-1; i>0; i--) if(a[i] != 0) { tt = i; break; } for(int i = tt; i > 0;i--) printf("%d",a[i]); puts(""); } }
\
本文已参与「新人创作礼」活动,一起开启掘金创作之路