题解 | #输出水仙花数#

输出水仙花数

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;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务