可計算任意位水仙花數
複製程式碼如下:
公共靜態無效主(字串[] args){
整數最大值 = 10;
for (int len = 1; len <= max; len++) {
System.out.println(getNarc(len, ""));
}
}
靜態 StringBuffer strb = new StringBuffer();
靜態字串 getNarc(int len, 字串開始) {
for (int basenum = 1; basenum <= 9; basenum++) {
if (len == 2 || len == 1) {
StringBuffer tmpbuf = new StringBuffer();
tmpbuf.append(開始);
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);
} 否則如果 (len > 2) {
字串 nextStr = "";
如果(空!=開始){
nextStr = 開始 + 基數;
} 別的 {
nextStr = Integer.toString(basenum);
}
getNarc(len - 2, nextStr);
}
}
返回 strb.toString();
}