题解 | #自守数#

自守数

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


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int count = 0;
            for (int i = 0; i <= n; i++) {
                if(isOK(i)){
                    count++;
                }
            }
            System.out.println(count);
        }
    }

    private static boolean isOK(int i) {
        if(i == 0 || i == 1){
            return true;
        }
        String sNew = i * i + "";
        String s = i + "";
        int sLen = s.length();
        int sNewLen = sNew.length();
        if(sNew.substring(sNewLen - sLen).equals(s)){
            return true;
        }
        return false;
    }
}
全部评论

相关推荐

尊尼获获:闺蜜在哪?
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务