水仙花数

水仙花数

http://www.nowcoder.com/questionTerminal/11c9f023a9f84418a15b48792a5f7c70

写一个辅助函数f来挨个判定即可,记得m和n都要包含,是闭区间

import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int m = sc.nextInt(), n = sc.nextInt();
            int r = 0;
            for(int i = m; i <= n; ++i){
                if(f(i)){
                    r++;
                    System.out.printf("%d ", i);
                }
            }
            if(r == 0){
                System.out.println("no");
                continue;
            }
            System.out.println();
        }
    }
    static boolean f(int n){
        int r = 0, t = n;
        while(t != 0){
            int a = t%10;
            r += (int)Math.pow(a, 3);
            t /= 10;
        }
        if(r == n) return true;
        return false;
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
05-11 20:45
门头沟学院 Java
有担当的灰太狼又在摸...:零帧起手查看图片
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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