The special palindrome number (java) has a clear structure

特殊回文数

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, find all such programming five and six decimal number satisfying the digits n equals
  solving ideas
  create two functions
  for summing for determining whether a palindrome is
  how to determine the The number of palindromes, here are two new strings, s1, s2 and
  s1 are placed directly , and s2 is placed backwards. Use java's equalsIgnoreCase to compare whether the two strings are equal.
  String + number is placed open, s1=s1+t; (t is number)
  Number + string is placed backwards s2=s1.charAt(i)+s2;

package 基础练习;

import java.util.Scanner;

public class 特殊回文数 {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		for(int i=10000;i<=999999;i++)
		{
    
    
			if(f(i)==n&&hui(i))
				System.out.println(i);
		}
	}
	static int f(int x) {
    
    //计算和
		int sum=0;
		while(x!=0) {
    
    
			int q=x%10;
			sum+=q;
			x=x/10;					
		}
		return sum;
	}
	static boolean hui(int t)//判断是否是回文数
	{
    
    
		String s1="";
		s1=s1+t;
		String s2="";
		for(int i=0;i<s1.length();i++)
			s2=s1.charAt(i)+s2;
		if(s1.equalsIgnoreCase(s2))
			return true;
		else
			return false;
	}
}

It's easy to sort out ideas

Guess you like

Origin blog.csdn.net/weixin_45079974/article/details/104876803