题解 | #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?

输入描述:

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, 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;
}




全部评论

相关推荐

找不到工作死了算了:没事的,雨英,hr肯主动告知结果已经超越大部分hr了
点赞 评论 收藏
分享
牛客717484937号:双飞硕没实习挺要命的
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务