自守数

自守数是指一个数的平方的尾数等于该数自身的自然数。例如:25^2 = 625,76^2 = 5776,9376^2 = 87909376。请求出n(包括n)以内的自守数的个数。

输入描述:

int型整数

输出描述:

n以内自守数的数量。

示例1

输入

6

输出

4

说明

有0,1,5,6这四个自守数

示例2

输入

1

输出

2

说明

有0, 1这两个自守数

Java 编程

package cn.net.javapub.demo2.demo;

/**
 * @author: shiyuwang
 */

import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while ((str = br.readLine()) != null) {
            int n = Integer.valueOf(str);
            int count = 0;
            for (int i = 0; i <= n; i++) {
                int temp = i;
                int j = 1;
                while (temp != 0) {
                    temp = temp / 10;
                    j = j * 10;
                }
                if ((i * i - i) % j == 0) {
                    count++;
                }
            }
            System.out.println(count);
        }
    }
}

展示效果:

华为OD机试 - 自守数 (Java 2024 E卷 100分)_Java

华为OD机试 - 自守数 (Java 2024 E卷 100分)_java_02