-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSACO9.java
44 lines (38 loc) · 1.22 KB
/
USACO9.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
USER: achyuta2
PROB: palsquare
LANG: JAVA
*/
import java.io.*;
public class palsquare {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("palsquare.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("palsquare.out")));
int x = Integer.parseInt(f.readLine());
String n;
int [] hi =new int[300];
for(int i=1;i<=300;i++){
n=BaseConvert(Integer.toString((int)Math.pow(i,2)),10,x );
if(palindrome(n)){
out.println(BaseConvert(Integer.toString(i),10,x).toUpperCase()+" "+n.toUpperCase());
}
}
out.close();
}
private static boolean palindrome(String str){
String reverse="";
for(int i = str.length() - 1; i >= 0; i--)
{
reverse = reverse + str.charAt(i);
}
if (reverse.equals(str)){
return true;
}
else{
return false;
}
}
public static String BaseConvert(String str, int fromBase, int toBase) {
return Integer.toString(Integer.parseInt(str, fromBase), toBase);
}
}