Dual+Palindromes

Dual Palindromes
A number that reads the same from right to left as when read from left to right is called a palindrome. The number 12321 is a palindrome; the number 77778 is not. Of course, palindromes have neither leading nor trailing zeroes, so 0220 is not a palindrome.

The number 21 (base 10) is not palindrome in base 10, but the number 21 (base 10) is, in fact, a palindrome in base 2 (10101).

Write a program that reads two numbers (expressed in base 10):


 * N (1 <= N <= 15)


 * S (0 < S < 10000)

and then finds and prints (in base 10) the first N numbers strictly greater than S that are palindromic when written in two or more number bases (2 <= base <= 10).

Solutions to this problem do not require manipulating integers larger than the standard 32 bits.

PROGRAM NAME: dualpal

INPUT FORMAT

A single line with space separated integers N and S.

SAMPLE INPUT (file dualpal.in)

3 25

OUTPUT FORMAT

N lines, each with a base 10 number that is palindromic when expressed in at least two of the bases 2..10. The numbers should be listed in order from smallest to largest.

SAMPLE OUTPUT (file dualpal.out)

26

27

28

Source Code

code format="java" /* ID: wjh3331 LANG: JAVA TASK: dualpal */

import java.io.*; import java.util.StringTokenizer;

public class dualpal { public static final int MIN_BASE = 2; // 최소 진법 public static final int MAX_BASE = 10; // 최대 진법 public static final int LEAST_PALINDROME = 2; // 최소한 나와야 하는 회문 개수

public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader("dualpal.in")); PrintWriter out = new PrintWriter(new FileWriter("dualpal.out"));

StringTokenizer st = new StringTokenizer(in.readLine); int n = Integer.parseInt(st.nextToken); // 출력 개수 int s = Integer.parseInt(st.nextToken); // 이거 다음 수 부터 시작

int count = 0; while(count < n) { s++;

int paled = 0; // 회문이 몇번 나왔는지 for(int i = MIN_BASE; i <= MAX_BASE; i++) { String represent = Integer.toString(s, i);

// 회문 판단 boolean pal = true; for(int j = 0; j < represent.length / 2; j++) { if(represent.charAt(j) != represent.charAt(represent.length - j - 1)) { pal = false; break; }       }        // 회문이면 카운트함. if(pal) { paled++; }     }

// 회문이 두번 이상 나왔으면 출력 if(paled >= LEAST_PALINDROME) { out.println(s); count++; }   }

out.close; System.exit(0); } } code

Comment 문제를 푸는데 least two 를 못봐서 땅속 외핵까지 삽질하였습니다.