USACO 1.2.5 Dual Palindromes JAVA

It is very similar to the previous 1.2.4.

The overall idea is not difficult. What you have to do is to save the input data first and record it as n and S, and then n is used for counting, and you can care about it later; and S is used to mark the starting point of the discussion, you can A for loop uses it. What we have to do now is to write a for loop. Look at the requirements of the topic. The for loop needs to start from S+1 and discuss the past one by one. For each number, you need to check to see if it can be presented in two bases Palindrome features, so you may need to create a Boolean variable such as sign to mark, change sign for the first time and continue to stop, and output and count for the second time.

package doublePalindromic;

import java.util.Scanner;

//输入数据
//	前n个 初始数字大于S
//	
//	在大于S的范围上一个一个讨论(考虑剪枝)
//		每一个数字二到十均转化一次进制
//		判断是不是回文
//		直到满足数字个数要求。

public class Main {
    
    
	static int n;
	static int S;

	public static void main(String[] args) {
    
    
		Scanner console= new Scanner(System.in);
		n= console.nextInt();
		S= console.nextInt();
		int countNum= 0;

		for(int i=S+1; ; i++) {
    
    
			if(countNum==n) return;
			//对于每一个数字 讨论各个进制下的回文情况
			boolean doubleSign1= false;

			for(int r=2; r<=10; r++) {
    
    
				String iUnderR= Integer.toString(i, r);
				StringBuffer reUnderR= new StringBuffer(iUnderR);
				if(iUnderR.equals(reUnderR.reverse().toString())) {
    
    
					if(!doubleSign1) {
    
    
						doubleSign1= true;
						continue;
					}
					else {
    
    
						System.out.println(i);
						countNum++;
						break;
					}
				}
			}
		
	}
	}
}


Guess you like

Origin blog.csdn.net/roswellnotfound/article/details/108961228