题解 | #水仙花数#C++暴力遍历水仙花数解法

水仙花数

https://www.nowcoder.com/practice/dc943274e8254a9eb074298fb2084703

#include <iostream>
#include <math.h>
using namespace std;

int main() 
{
    int i=0;
    int count=0;
    int m=0;
    int n=0;
    while(cin>>m>>n)
    {
        if(count)
        {
            cout<<endl;
        }
        count=1;
        int count2=0;
        for(i=m;i<=n;i++)
        {
            int k=i;
            int z=0;
            while(k)
            {
                int x=k%10;
                z+=pow(x,3);
                k/=10;
            }
            if(z==i)
            {
                if(count2)
                {
                    cout<<" "<<i;
                }
                else 
                {
                    cout<<i;
                }
                count2++;
            }
        }
        if(count2==0)
        {
            cout<<"no";
        }
    }
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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