题解 | #Factorial#
Factorial
https://ac.nowcoder.com/acm/problem/14323
题目描述
Hill was a clever boy,he like math very much.Today teacher give him a question.
calculate N! . But Hill was tired,he need to sleep,so let you help him to calculate N!.
what is N!
N! = 1*2*3*......*N
Hey, you need to think about 0! . Do you?
calculate N! . But Hill was tired,he need to sleep,so let you help him to calculate N!.
what is N!
N! = 1*2*3*......*N
Hey, you need to think about 0! . Do you?
输入描述:
There are multiple test cases. The first line is an positive integer T indicating the number of test cases.(0<T<=1000)
For each test case:
A positive integer N(0<=N<=20)
For each test case:
A positive integer N(0<=N<=20)
输出描述:
For each test case, output one line.
用递归方法计算阶乘,remember:阶乘类型要用long long,不然会溢出
#include<iostream> using namespace std; long long Calculate(int n) { if(!n) return 1; else return n*Calculate(n-1); } int main() { int n; cin>>n; int sum; while(n--) { cin>>sum; cout<<Calculate(sum)<<endl; } return 0; }