首页 > 解决方案 > Codechef 拒绝(数字的阶乘)

问题描述

为什么 codechef 拒绝此解决方案,即使它在 codechef 代码运行程序本身上提供了理想的输出?(数字的阶乘)

#include <iostream>
using namespace std;

int main() {
    
int t;
cin>>t;
int n;
int fact=1;

for(int i=0; i<t;i++){
    cin>>n;
    for(int i=1; i<=n; i++){
        fact=fact*i;
    }cout<<fact;
    cout<<endl;
    fact=1;
    
}
return 0;
}

标签: c++c++17factorial

解决方案


由于您将阶乘的结果存储在int最大限制为 的变量中2147483647,因此对于大于此的数字,阶乘将无法正确计算。

尝试使用可以具有最大上限的其他数据类型。https://docs.microsoft.com/en-us/cpp/c-language/cpp-integer-limits?view=msvc-160


推荐阅读