水仙花数,所谓水仙花数即一个三位数的个位立方+十位立方+百位立方与自身相等!

水仙花数,所谓水仙花数即一个三位数的个位立方+十位立方+百位立方与自身相等!

C++代码参考

#include <iostream>
#include <cmath>
using namespace std;
 
int main(int argc, const char * argv[]) {
    int n, unit, ten, hund; // unit, ten, hund分别存储个位、十位和百位的数字
    
    for(n = 100; n < 1000; n++) {
        unit = n % 10; // 个位
        ten = (n / 10) % 10; // 十位
        hund = n / 100; // 百位
        // 判断各位数字的立方和是否等于它本身
        if(n == hund * hund * hund + ten * ten * ten + unit * unit * unit)
            cout << n << " ";
    }
    cout << endl;
    return 0;
}