c++ - 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;
}
解决方案
由于您将阶乘的结果存储在int
最大限制为 的变量中2147483647
,因此对于大于此的数字,阶乘将无法正确计算。
尝试使用可以具有最大上限的其他数据类型。https://docs.microsoft.com/en-us/cpp/c-language/cpp-integer-limits?view=msvc-160
推荐阅读
- c++ - 当为可变性配置时, boost::heap::d_ary_heap 持有的额外 std::list 的目的是什么?
- android - 字符串 XML 常量未在 xml 文件中初始化值
- javafx - 用于桌面应用程序的使用 charm-glisten.jar 的 JavaFX Gluon 进度指示器
- javascript - 如何在不重写所有路由的情况下禁用 Express 中单个路由的 CORS?
- join - 在 Rails 上加入 3 个表
- python - 洪水填充算法 - 我的实现中的错误
- r - 许多列的condformat
- java - 无法膨胀我的 SwipeRefreshLayout。没有任何东西阻止 UI,没有显示任何错误,并且所有内容都已迁移到 androidx。有任何想法吗?
- python - 错误:“numpy.ndarray”对象没有属性“模式”
- ios - 如何将 NSNumber 截断/舍入到小数点后一位