如果自然数 n > 0 的真约数之和(包括 1 但不包括 n 本身)大于其自身,则称该自然数为丰富数。例如,数字12是一个丰富的数字,因为它的约数之和(包括1但不包括12本身)是1+2+3+4+6=16,大于12本身。作为对比,数字 6 不是一个丰富的数字,因为它的约数之和(包括 1 但不包括 6 本身)是 1+2+3=6,它不大于 6 本身。在此处查看更多示例和说明。
好像每次用户输入一个数字,它总是很丰富。对需要做什么有什么建议吗?
// If the user selects option "a"
if (option == 'a')
{
bool abundantTest(int n);
{
int n, i = 1, sum = 0;
cout << "Enter a number: " << endl;
cin >> n;
while (i < n){
if (n % i == 0)
sum = sum + i;
i++;
}
if (sum == n){
cout << i << " is not an abundant number." << endl;
}
else{
cout << i << " is an abundant number." << endl;
}
}
}
最佳答案
也许你只是想检查一下
if (sum <= n)
小于或等于与大于相反。
你还可以:
if (sum > n)
cout << i << " is an abundant number." << endl;
else
cout << i << " is not an abundant number." << endl;
简单地说:
cout << i << " is " + std::string(sum > n ? "" : "not ") + "an abundant number." << endl;
关于c++ - 丰富的数字测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34245938/