ZOJ 1078

    技术2024-08-10  61

    Palindrom Numbers
    Time Limit: 1 Second      Memory Limit: 32768 KB

    Statement of the Problem

    We say that a number is a palindrom if it is the sane when read from left to right or from right to left. For example, the number 75457 is a palindrom.

    Of course, the property depends on the basis in which is number is represented. The number 17 is not a palindrom in base 10, but its representation in base 2 (10001) is a palindrom.

    The objective of this problem is to verify if a set of given numbers are palindroms in any basis from 2 to 16.

    Input Format

    Several integer numbers comprise the input. Each number 0 < n < 50000 is given in decimal basis in a separate line. The input ends with a zero.

    Output Format

    Your program must print the message Number i is palindrom in basis where I is the given number, followed by the basis where the representation of the number is a palindrom. If the number is not a palindrom in any basis between 2 and 16, your program must print the message Number i is not palindrom.

    Sample Input

    17 19 0

    Sample Output

    Number 17 is palindrom in basis 2 4 16 Number 19 is not a palindrom

     


    用java 类提供的方法写的 方便

    package zoj; import java.util.Scanner; public class Zoj1078 { public static boolean isHuiwen(String s) { StringBuffer buf = new StringBuffer(s); buf = buf.reverse(); return s.equals(buf.toString()); } public static String jinZhi(int num, int i) { String s = Integer.toString(num, i); return s; } public static void main(String[] agrs) { Scanner s = new Scanner(System.in); int num = s.nextInt(); int[] k = new int[17]; while (num != 0) { for (int i = 0; i < k.length; i++) { k[i] = 0; } boolean ok = false; for (int i = 2; i <= 16; i++) { if (isHuiwen(jinZhi(num, i)) == true) { k[i] = 1; ok = true; } } if (ok == false) { System.out.println("Number " + num + " is not a palindrom"); } else { System.out.print("Number " + num + " is palindrom in basis"); for (int i = 2; i < k.length; i++) { if (k[i] == 1) { System.out.print(" " + i); } } System.out.println(); } num = s.nextInt(); } } }

    最新回复(0)