java计算任意位水仙花数示例(回文数)
可计算任意位水仙花数
public static void main(string[] args) {
int max = 10;
for (int len = 1; len <= max; len++) {
system.out.println(getnarc(len, ""));
}
}
static stringbuffer strb = new stringbuffer();
static string getnarc(int len, string start) {
for (int basenum = 1; basenum <= 9; basenum++) {
if (len == 2 || len == 1) {
stringbuffer tmpbuf = new stringbuffer();
tmpbuf.append(start);
for (int j = 0; j < len; j++) {
tmpbuf.append(basenum);
}
tmpbuf.append(new stringbuffer(start).reverse().tostring());
strb.append(tmpbuf);
strb.append("\n");
// system.out.println(tmpbuf);
} else if (len > 2) {
string nextstr = "";
if (null != start) {
nextstr = start + basenum;
} else {
nextstr = integer.tostring(basenum);
}
getnarc(len - 2, nextstr);
}
}
return strb.tostring();
}
上一篇: asp.net操作xml增删改示例分享
下一篇: 深入浅析Android消息机制