题解 | #输出水仙花数#
输出水仙花数
https://www.nowcoder.com/practice/dabaf13009ef4d0cbf22302fd6a530a6
#include <iostream>
using namespace std;
#include<math.h>
//选用bool函数,bool类型变量的值只有真(1)和假(0)
bool isNarcissus(int num) {
    int h = num / 100;  //百位:151/100=1;
    int t = (num / 10) % 10; //十位:151/10=15;15%10=5;
    int o = num % 10;   //个位:151%10=1;
    //pow(m,n)-->m的n次幂  要加上math头文件
    if (num == pow(h, 3) + pow(t, 3) + pow(o, 3))
        return true;
    else
        return false;
}
int main() {
    // write your code here......
    for(int i=100;i<=999;i++)
      if(isNarcissus(i))
        cout << i << endl;
    return 0;
}
 查看9道真题和解析
查看9道真题和解析