PAT 1091 N-自守数 (15 分) Java实现

PAT 1091 N-自守数 (15 分) Java实现_第1张图片

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

/**
 * @date 2021-8-19 - 18:58
 * Created by Salmon
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(bufferedReader.readLine());
        String[] strings = bufferedReader.readLine().split("\\s+");
        bufferedReader.close();
        for(int i = 0; i < n; i++) {
            fun(strings[i].length(),Integer.parseInt(strings[i]));
        }
    }

    public static void fun(int length,int num){
        int flag = 0;
        for(int N = 1; N <= 10; N++) {
            String temp = String.valueOf((int) Math.pow(num, 2) * N);
            String lastDig = temp.substring(temp.length() - length);
            if(num == Integer.parseInt(lastDig)){
                flag++;
                System.out.println(N + " " + temp);
                break;
            }
        }
        if(flag == 0){
            System.out.println("No");
        }
    }
}

PAT 1091 N-自守数 (15 分) Java实现_第2张图片

你可能感兴趣的:(PAT,算法)