题解 | #输出水仙花数#
输出水仙花数
https://www.nowcoder.com/practice/dabaf13009ef4d0cbf22302fd6a530a6
#include <iostream>
#include <cmath>
#include <cstdbool>
using namespace std;
bool narcissisticNumber(int n);
int main() {
int n;
cin >> n;
for(int i = 100;i<999;i++)
{
if(narcissisticNumber(i))
cout << i << endl;
}
return 0;
}
bool narcissisticNumber(int n)
{
int individual = n % 10;
int tenth = (n/10) % 10;
int hundredPlaces = n / 100 % 10;
if (pow(individual,3) + pow(tenth,3) + pow(hundredPlaces,3) == n)
return true;
else
return false;
}
查看13道真题和解析