本帖最后由 冯海霞 于 2012-10-29 23:11 编辑
前几天已经写好了,因为没时间优化和回帖,今天想回却关闭了,反正都写了帖出来互相学习一下,哪个兄弟有更简单的方法就帖出来大家学习一下~~~:D
class Test11
{
public static void main(String[] args)
{
Test(5, 3);
}
public static void Test(int bit, int HWBit)
{
String strMax = "";
String intStr = "";
String reStr = "";
int count = 0;
int hw = 0;
StringBuilder sb = new StringBuilder();
for(int x=1; x<=bit; x++)
{
strMax += "7";
}
int intMax = new Integer(strMax);
for(int x=1; x<=intMax; x++)
{
if(String.valueOf(x).contains("8") || String.valueOf(x).contains("9"))
continue;
if(x%2 != 0)
count++;
intStr = String.valueOf(x);
reStr = sb.append(intStr).reverse().toString();
if(intStr.length()==HWBit && intStr.equals(reStr))
{
sop(intStr);
hw++;
}
sb.delete(0, sb.length());
}
sop(bit+"位及以下奇数的个数:"+count);
sop("回文数:"+hw);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
5位及以下奇数的个数:16384
回文数:56
|