c++ - 当除数之和不大于数字时,程序将不接受大于 10000 的限制

标签 c++

当我将限制从 100 更改为 1,000,000 时,它不会打印出所有数字,它会停在 5 位数字上并且不会计算更高的数字。这是为什么?

#include <iostream>
using namespace std;

int main(int argc, char* argv[]) 
{

const int limit = argc > 1 ? atoi(argv[1]) : 100;
const int badness = argc > 2 ? atoi(argv[2]) : 1;


for (int num = 2; num < limit; num++) {

    int total = 1;

    for (int factor = 2; factor * factor <= num; factor++) {

        if (num % factor == 0) {
            total += factor;


            if (factor * factor != num) {
                total += (num / factor);
            }
        }


        }

        if (abs(num - total) <= abs(badness)) {

            cout << num << " " << flush;
        }
    }
}

最佳答案

看看这个 https://en.wikipedia.org/wiki/C_data_types

这可能是因为您正在使用 int。

尝试使用 long 或 unsigned long

关于c++ - 当除数之和不大于数字时,程序将不接受大于 10000 的限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55320886/

相关文章:

c++ - "static"关键字与变量、函数和类对象等有什么用?

c++ - 如何使用 string::replace 为 char*

c++ - 无法从主 C++ 获取要打印的链表

c++ - 如果对 QObject::connect() 使用 lambda 函数,则使用 gcc 编译会失败

c++ - Mat数据结构(数组)初始化错误

c++ - 在原始循环上使用 boost::irange 的性能损失

c++ - 外部指针初始化

c++ - 如何将 int 转换为枚举值 C++

C++归纳算法非常慢和动态编程

c++ - CMake 使用绝对路径导出目标