题解 | #水仙花数#

水仙花数

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int m = sc.nextInt();
            int n = sc.nextInt();
            boolean isFlag = false; // 是否存在水仙花数
            for (int i = m; i <= n ; i++) {
                int sum = 0;
                String s = String.valueOf(i);
                for (int j = 0 ; j < s.length(); j++) {
                    sum += Math.pow(Integer.valueOf(s.charAt(j) + ""), 3);
                }
                if (sum == i) {
                    isFlag = true;
                    System.out.print(i + " ");
                }
            }
            if (!isFlag) {
                System.out.println("no");
            } else {
                System.out.println();
            }
        }
    }
}
全部评论

相关推荐

uu们,拒offer时hr很生气怎么办我哭死
爱睡觉的冰箱哥:人家回收你的offer,或者oc后没给你发offer的时候可不会愧疚你,所以你拒了也没必要愧疚他。
点赞 评论 收藏
分享
07-02 13:50
闽江学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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