题解 | #自守数#

自守数

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static boolean isAutoMorphic(int num) {
        int square = (int) Math.pow(num, 2);
        // num的位数
        int digit = 0;
        int backup = num;
        while (backup != 0) {
            digit++;
            backup /= 10;
        }
        // 模几
        int mod = (int) Math.pow(10, digit);
        return square % mod == num;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int ans = 0;
        for (int i = 0; i <= n; i++) {
            if (isAutoMorphic(i)) {
                ans++;
            }
        }
        System.out.println(ans);
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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