如果自然数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;
}
}
}发布于 2015-12-12 22:54:03
也许你只是想检查一下
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;https://stackoverflow.com/questions/34245938
复制相似问题