c++,水仙花数是指一个3位数,它的每个位上的数字的3次幂之和等于它本身。 编写程序,打印水仙花散。
时间: 2024-10-22 07:07:21 浏览: 74
在C++中,我们可以编写一个简单的程序来查找并打印所有的水仙花数。水仙花数也称为 Narcissistic Number 或者 Pluperfect Digital Root,其特点是每一位的立方和等于该数本身。以下是创建这个程序的一个例子:
```cpp
#include
// Function to check if a number is a narcissistic number bool isNarcissistic(int num) { int sum = 0; int temp = num;
// Calculate the sum of cubes of each digit
while (temp > 0) {
int digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
// If the sum equals the original number, it's a narcissistic number
return (sum == num);
}
int main() { for (int i = 100; i <= 999; ++i) { // Iterate through 3-digit numbers if (isNarcissistic(i)) { cout << i << " "; } }
cout << endl;
相关推荐


















