chance2000
发贴: 5
积分: 0
|
于 2006-05-24 18:16
//替换字符串中的index位为newChar public static String replaceIndex(String oldStr, int index, char newChar) { return oldStr.substring(0, index) + newChar + oldStr.substring(index + 1, oldStr.length()); } //生成x位的A-Z的字串 public static void doIt(int n) { int idx = 0; String start = ""; String[] arr = new String[26]; StringBuffer sb = new StringBuffer(2048);
for (int i = 0; i < n; i++) { start = start + "A"; }
for (char c = 'A'; c <= 'Z'; c++) { arr[idx] = replaceIndex(start, n - 1, c); sb.append(arr[idx]); sb.append(" "); idx++; } sb.append("\n"); idx = 0; n--; while ( (--n) >= 0) { for (char c = 'B'; c <= 'Z'; c++) { for (int i = 0; i < 26; i++) { String tmp = replaceIndex(arr[i], n, c); sb.append(tmp); sb.append(" "); if (c == 'Z') { arr[i] = tmp; } idx++; } sb.append("\n"); } } System.out.println(sb.toString()); }
public static void main(String args[]) { doIt(4); }
这个不知道是不是你要的结果?这个效率应该还算可以。
|