Skip to content

1091 N-自守数

如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”。例如 3×922=25392,而 25392 的末尾两位正好是 92,所以 92 是一个 3-自守数。

本题就请你编写程序判断一个给定的数字是否关于某个 NN-自守数。

输入格式:

输入在第一行中给出正整数 M(≤20),随后一行给出 M 个待检测的、不超过 1000 的正整数。

输出格式:

对每个需要检测的数字,如果它是 N-自守数就在一行中输出最小的 NN**K2 的值,以一个空格隔开;否则输出 No。注意题目保证 N<10。

输入样例:

tex
3
92 5 233
3
92 5 233

输出样例:

tex
3 25392
1 25
No
3 25392
1 25
No

Solution:

java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int m = Integer.parseInt(in.readLine());
        String[] input = in.readLine().split(" ");
        label:
        for (int i = 0; i < input.length; i++) {
            int num = Integer.parseInt(input[i]);
            for (int j = 1; j < 10; j++) {
                int x = j * num * num;
                if (Integer.toString(x).endsWith(Integer.toString(num))) {
                    System.out.printf("%d %d\n", j, x);
                    continue label;
                }
            }
            System.out.println("No");

        }
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int m = Integer.parseInt(in.readLine());
        String[] input = in.readLine().split(" ");
        label:
        for (int i = 0; i < input.length; i++) {
            int num = Integer.parseInt(input[i]);
            for (int j = 1; j < 10; j++) {
                int x = j * num * num;
                if (Integer.toString(x).endsWith(Integer.toString(num))) {
                    System.out.printf("%d %d\n", j, x);
                    continue label;
                }
            }
            System.out.println("No");

        }
    }
}

Released under the MIT License.