题解 | #自守数#

自守数

http://www.nowcoder.com/practice/88ddd31618f04514ae3a689e83f3ab8e

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int num=0;
            for(int i=0;i<=a;i++){
                if(isSelf(i)){
                    num++;
                }
            }
            System.out.println(num);
        }
    }
    //判断是否是自留数
    public static boolean isSelf(int a){
        int b=a*a;
        while(a!=0){
            if(a%10!=b%10){
                return false;
            }else{
                a/=10;
                b/=10;
            }
        }
        return true;
    }
全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务