题解 | #输出水仙花数#
输出水仙花数
https://www.nowcoder.com/practice/dabaf13009ef4d0cbf22302fd6a530a6
#include <iostream> #include <cmath> using namespace std; bool isNarcissus(int i) { int number = i; int n = number; int pos = 0; while(n != 0) { n/=10; ++pos; } int sum = 0; sum+=pow(number%10,pos); sum+=pow(number/10%10,pos); sum+=pow(number/100,pos); if(sum == number) { return true; } else { return false; } } int main() { // write your code here...... for(int i=100;i<1000;i++) { if(isNarcissus(i)) { cout<<i<<endl; } } return 0; }