Special palindrome number for basic exercises of Lanqiao Cup (Java solution)

Problem Description

123321 is a very special number. Reading it from the left is the same as reading it from the right.
Enter a positive integer n, and program to find all such five-digit and six-digit decimal numbers so that the sum of each digit is equal to n.

Input format

Enter a line containing a positive integer n.

Output format

Output the integers that meet the conditions in ascending order, and each integer occupies one line.

Sample input

52

Sample output

899998
989989
998899

Data size and convention

1<=n<=54。

Simple implementation

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main{
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int hw: hwNum(n)) {
    
    
            System.out.println(hw);
        }
    }

    /**
     * 五到六位特殊回文数
     * @return 特殊回文数列表
     */
    public static List<Integer> hwNum(int n){
    
    
        List<Integer> hws = new ArrayList<Integer>();
        for (int i = 10000; i < 1000000; i++) {
    
    
            String str = i + "";
            int flag = -1;
            int midIndex = str.length() / 2;
            for (int j = 0; j < midIndex; j++) {
    
    
                if(str.charAt(j) == str.charAt(str.length()-j-1)) flag = 1;
                else {
    
    
                    flag = -1;
                    break;
                }
            }

            if(flag == 1){
    
    
                int sum = 0;
                for (int j = 0; j < str.length(); j++) {
    
    
                    sum += Integer.parseInt(str.charAt(j)+"");
                }
                if(sum == n){
    
    
                    hws.add(i);
                }
            }
        }
        return hws;
    }
}

Guess you like

Origin blog.csdn.net/L333333333/article/details/103889578