若三位数ABC满足ABC=A3+B3+C3,则称ABC为水仙花数. 例如153就是一个水仙花数. 编程找出100~999范围内的所有水仙花数.
代码实现:
// 水仙花数
#include<iostream>
#include<cmath>
using namespace std;
int main() {
int a, b, c;
int result = 0;
int num;
int temp;
for (int i = 100; i <= 999; ++i) {
num = i;
temp = i;
a = num%10;
num /= 10;
b = num%10;
num /= 10;
c = num%10;
if (a*a*a + b*b*b + c*c*c == temp)
cout << temp << endl;
}
return 0;
}