Created
April 15, 2011 04:33
-
-
Save stphung/921144 to your computer and use it in GitHub Desktop.
My solution to "Palindromic Squares" from USACO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| ID: stphung1 | |
| LANG: JAVA | |
| TASK: palsquare | |
| */ | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.io.PrintWriter; | |
| import java.util.Scanner; | |
| class palsquare { | |
| public static void main(String[] args) throws IOException { | |
| Scanner sc = new Scanner(new File("palsquare.in")); | |
| PrintWriter pw = new PrintWriter(new File("palsquare.out")); | |
| int base = sc.nextInt(); | |
| char[] rep = new char[] {'0','1','2','3','4','5','6','7','8','9', | |
| 'A','B','C','D','E','F','G','H','I','J'}; | |
| for(int i=1; i<=300; i++) { | |
| String square = toBase(i*i,base,rep); | |
| if (isPalindrome(square)) { | |
| pw.println(toBase(i,base,rep) + " " + square); | |
| } | |
| } | |
| pw.close(); | |
| } | |
| private static boolean isPalindrome(String input) { | |
| for(int i=0; i<input.length(); i++) { | |
| if (input.charAt(i) != input.charAt(input.length()-i-1)) return false; | |
| } | |
| return true; | |
| } | |
| private static String toBase(int i, int base, char[] rep) { | |
| StringBuilder sb = new StringBuilder(); | |
| int val = i; | |
| while(val >= base) { | |
| sb.insert(0, rep[val%base]); | |
| val = val/base; | |
| } | |
| return sb.insert(0, rep[val]).toString(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment