🌕🌑🌑🌑🌑

# 題目連結

  • 題目連結
  • Online Judge
  • uDebug

# 題目說明

Time limit: 3.000 seconds

# 題目

The number 3025 has a remarkable quirk: if you split its decimal representation in two strings of equal length ( 30 and 25 ) and square the sum of the numbers so obtained, you obtain the original number:

(30+25)2=3025(30 + 25)^2 = 3025

The problem is to determine all numbers with this property having a given even number of digits.

For example, 4-digit numbers run from 0000 to 9999 . Note that leading zeroes should be taken into account. This means that 0001 which is equal to (00+01)2(00 + 01)^2 is a quirksome number of 4 digits. The number of digits may be 2 , 4 , 6 or 8 . Although maxint is only 32767 and numbers of eight digits are asked for, a well-versed programmer can keep his numbers in the range of the integers. However efficiency should be given a thought.

# Input

The input of your program is a textfile containing numbers of digits (taken from 2 , 4 , 6 , 8 ), each number on a line of its own.

# Output

The output is a textfile consisting of lines containing the quirksome numbers (ordered according to the input numbers and for each input number in increasing order).

Warning: Please note that the number of digits in the output is equal to the number in the corresponding input line : leading zeroes may not be suppressed.

# Sample Input

2
2

# Sample Output

00
01
81
00
01
81

# 解題技巧

利用運算解題,基本上就是直覺解。

# Solution

Main.java
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Map<Integer, ArrayList<String>> map = new HashMap<>();
        for(int i = 2; i <= 8; i = i + 2){
            int pow = (int)Math.pow(10, i / 2);
            ArrayList<String> temp = new ArrayList<>();
            for(int a = 0; a < pow; a++){
                for(int b = 0; b < pow; b++){
                    if(a * pow + b == Math.pow(a + b, 2)){
                        String t = Integer.toString(a * pow + b);
                        while(t.length() < i){
                            t = "0" + t;
                        }
                        temp.add(t);
                    }
                }
            }
            map.put(i, temp);
        }
        while(sc.hasNextInt()){
            int num = sc.nextInt();
            ArrayList<String> ans = map.get(num);
            for(String s : ans){
                System.out.println(s);
            }
        }
        sc.close();
    }
}
單字

** **
!! !!

片語 & 搭配詞

!! !!