题解 | #自守数#

自守数

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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int count = 0;
        for (int i = 0; i <= n; i++) {
            String s = String.valueOf(i);
            if (String.valueOf(s.charAt(s.length() - 1)).matches("^[0]|[1]|[5]|[6]$")) {
                int product = i * i;
                if (check(s, String.valueOf(product))) {
                    count++;
                }
            }
        }
        System.out.println(count);
    }

    private static boolean check(String s1, String s2) {
        int length1 = s1.length();
        int length2 = s2.length();
        for (int i = 0; i < length1; i++) {
            if (s1.charAt(length1 - 1 - i) != s2.charAt(length2 - 1 - i)) {
                return false;
            }
        }
        return true;
    }
}

解题思路:

1, 逐个遍历, 核查当前数字是否是自守数;

2, 注意自守数尾数必须是0 1 5 6

全部评论

相关推荐

不愿透露姓名的神秘牛友
01-22 18:07
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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