[Cup] Blue Bridge palindromic numbers (violence simple questions)

Title Description

Observation Number: 12321,123321 have a common feature, both read from left to right or right to left reading, are the same. Such numbers called: palindrome numbers.

This question asks you to find some five or six decimal digits. Meet the following requirements:
integer digit of each of the digital input is equal.
Enter
a positive integer n (10 <n <100) , to meet the requirements represented and digital.
Output
a plurality of rows, each row comprising five or six integer meet the requirements.
From small to large numbers arranged in the order.
If the condition is not satisfied, output: -1
Sample Input
44
Sample Output
99 899
499 994
589 985
598 895
679 976
688 886
697 796
769 967
778 877
787 787
796 697
859 958
868 868
877 778
886688
895 598
949 949
958 859
967 769
976 679
985 589
994 499

Problem-solving ideas

DFS had wanted to use, requires only later found palindromic digital output is 5 or 6
then use the three cycles from cycle 1 to 9
to 5 and then count the calculated position 6
but three wa, a leak was found case, the palindromic numbers first and last can not be zero, other numbers can be 0.

Code

package 回文数字;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int x,y;
		int sum=0;
		for(int i=1;i<10;i++)
		{
			x=i;
			n=n-2*i;
			if(n>=0)
			{
				for(int j=0;j<10;j++)
				{
					y=j;
					n=n-2*j;
					if(n>=0)
					{	
						for(int k=0;k<10;k++)
						{
							if(k==n)
							{
								sum+=1;
								System.out.println(x*10000+y*1000+k*100+y*10+x);
							}
						}
					}
					n=n+2*j;
				}
			}
			n=n+2*i;
		}
		for(int i=1;i<10;i++)
		{
			x=i;
			n=n-2*i;
			if(n>=0)
			{
				for(int j=0;j<10;j++)
				{
					y=j;
					n=n-2*j;
					if(n>=0)
					{	
						for(int k=0;k<10;k++)
						{
							if(2*k==n)
							{
								sum+=1;
								System.out.println(x*100000+y*10000+k*1000+k*100+y*10+x);
							}
						}
					}
					n=n+2*j;
				}
			}
			n=n+2*i;
		}
		if(sum==0)
		{
			System.out.println(-1);
		}
	}

}

Published 20 original articles · won praise 0 · Views 3865

Guess you like

Origin blog.csdn.net/weixin_44544406/article/details/104216163