题解 | #输出水仙花数#

输出水仙花数

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

全部评论

相关推荐

2025-11-06 18:28
南京大学 后端工程师
面了100年面试不知...:被割穿了才想起来捞人了
投递哔哩哔哩等公司8个岗位
点赞 评论 收藏
分享
2025-12-27 22:35
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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