题解 | #自守数#

自守数

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            int num = 1;
            for (int i = 1; i <= n; i++) {
                if (ziShouShu(i)) {
                    num++;
                }
            }
            System.out.println(num);
        }
    }

    public static boolean ziShouShu(int n) {
        int m = n * n;
        int quyu = 1;
        for (int i = 0; i < weiShu(n); i++) {
            quyu = quyu * 10;
        }
        if (n == m % quyu) {
            return true;
        } else {
            return false;
        }


    }

    public static int weiShu(int n) {
        int count = 0;
        while (n > 0) {
            n = n / 10;
            count++;
        }
        return count;

    }
}

全部评论

相关推荐

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