USACO_1_2_Palindromic Squares

 

ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
ID: sdjllyh1
PROG: palsquare
LANG: JAVA
complete date: 2008/9/25
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/


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

public   class  palsquare
ExpandedBlockStart.gifContractedBlock.gif
{
    
private static int base;
    
    
public static void main(String[] args) throws IOException
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        init();
        runAndOutput();
        System.exit(
0);
    }


    
private static void init() throws IOException
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        BufferedReader f 
= new BufferedReader(new FileReader("palsquare.in"));
        base 
= Integer.parseInt(f.readLine());
        f.close();
    }


    
private static void runAndOutput() throws IOException
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        String[] squaresInBase 
= new String[301];
        
for (int i = 1; i <= 300; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            squaresInBase[i] 
= getNumberInBase(i * i);
        }


        PrintWriter out 
= new PrintWriter(new BufferedWriter(new FileWriter("palsquare.out")));
        
for (int i = 1; i <= 300; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (isPalindrome(squaresInBase[i]))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                out.print(getNumberInBase(i));
                out.print(
" ");
                out.println(squaresInBase[i]);
            }

        }

        out.close();
    }


    
private static String getNumberInBase(int value) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        String retNumberInBase 
= "";
        
while (value > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            retNumberInBase 
= numToLetter(value % base) + retNumberInBase;
            value 
= value / base;
        }

        
return retNumberInBase;
    }


    
private static boolean isPalindrome(String num)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
int length = num.length();
        
for (int i = 0; i < length /2; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (num.charAt(i) != num.charAt(length - i -1))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return false;
            }

        }

        
return true;
    }


    
private static String numToLetter(int value)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
char retLetter ;
        
if (value<10)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            retLetter 
= '0';
            retLetter 
+= value;
        }

        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            retLetter 
= 'A';
            retLetter 
+= value - 10;
        }


        
return String.valueOf(retLetter);
    }

}

转载于:https://www.cnblogs.com/SDJL/archive/2008/09/25/1298769.html

你可能感兴趣的:(java)