题解 | #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;
}




全部评论

相关推荐

废铁汽车人:秋招真是牛鬼蛇神齐聚一堂
点赞 评论 收藏
分享
伟大的烤冷面被普调:暨大✌🏻就是强
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务