题解 | 水仙花数

水仙花数

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

#include <stdio.h>
#include "math.h"
int shui(int n){
    int weight=0;
    int t=n;
    while(t>0){
        weight++;
        t/=10;
    }
    t=n;
    int sum=0;
    while(t>0){
        sum+=pow(t%10,weight);
        t/=10;
    }
    if(sum==n){
        return 1;
    }else{
        return 0;
    }
}
int main() {
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF){
        int sign=0;
        for(int i=n;i<=m;i++){
            if(shui(i)){
                sign=1;
                printf("%d ",i);
            }
        }
        if(!sign){
            printf("no");
        }
        printf("\n");
    }
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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