会员
众包
新闻
博问
闪存
赞助商
HarmonyOS
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
SDJL的足迹
首页
博问
闪存
管理
USACO_1_2_Palindromic Squares
/**/
/*
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
{
private
static
int
base;
public
static
void
main(String[] args)
throws
IOException
{
init();
runAndOutput();
System.exit(
0
);
}
private
static
void
init()
throws
IOException
{
BufferedReader f
=
new
BufferedReader(
new
FileReader(
"
palsquare.in
"
));
base
=
Integer.parseInt(f.readLine());
f.close();
}
private
static
void
runAndOutput()
throws
IOException
{
String[] squaresInBase
=
new
String[
301
];
for
(
int
i
=
1
; i
<=
300
; i
++
)
{
squaresInBase[i]
=
getNumberInBase(i
*
i);
}
PrintWriter out
=
new
PrintWriter(
new
BufferedWriter(
new
FileWriter(
"
palsquare.out
"
)));
for
(
int
i
=
1
; i
<=
300
; i
++
)
{
if
(isPalindrome(squaresInBase[i]))
{
out.print(getNumberInBase(i));
out.print(
"
"
);
out.println(squaresInBase[i]);
}
}
out.close();
}
private
static
String getNumberInBase(
int
value)
{
String retNumberInBase
=
""
;
while
(value
>
0
)
{
retNumberInBase
=
numToLetter(value
%
base)
+
retNumberInBase;
value
=
value
/
base;
}
return
retNumberInBase;
}
private
static
boolean
isPalindrome(String num)
{
int
length
=
num.length();
for
(
int
i
=
0
; i
<
length
/
2
; i
++
)
{
if
(num.charAt(i)
!=
num.charAt(length
-
i
-
1
))
{
return
false
;
}
}
return
true
;
}
private
static
String numToLetter(
int
value)
{
char
retLetter ;
if
(value
<
10
)
{
retLetter
=
'
0
'
;
retLetter
+=
value;
}
else
{
retLetter
=
'
A
'
;
retLetter
+=
value
-
10
;
}
return
String.valueOf(retLetter);
}
}
posted on
2008-09-25 14:51
刘永辉
阅读(
302
) 评论(
0
)
收藏
举报
刷新页面
返回顶部