Topic: 如何生成x位A-Z的字串呢? |
Print this page |
1.如何生成x位A-Z的字串呢? | Copy to clipboard |
Posted by: binge Posted on: 2006-05-17 12:29 //现有需求如下,生成x位的A-Z的字串, //x位正整数 //如果为2位,则int x=2; //生成的字串应该为AA AB AC AE....ZZ //如果为5位,则int x=5; //生成的字串应该为AAAAA AAAAB AAAAC AAAAD AAAAE...ZZZZZ //如果为7位,则int x=7; //生成的字串应该为AAAAAAA AAAAAAB AAAAAAC AAAAAAD...ZZZZZZZ //不知是否明白我的意思? //如果只生成一位可以解决,在两位以上的话就不好解决了, //要生成A-Z(A到Z之间的所有字符)可以用 for (int i='A';i<='Z';i++){ char atoz=(char)i; System.out.println(atoz); } //谢谢各位 |
2.Re:如何生成x位A-Z的字串呢? [Re: binge] | Copy to clipboard |
Posted by: why Posted on: 2006-05-18 18:15 Are you sure you want to print all those 字串?? x num of 生成的字串 1 26 2 676 3 17576 4 456976 5 11881376 6 308915776 7 8031810176 |
3.Re:如何生成x位A-Z的字串呢? [Re: binge] | Copy to clipboard |
Posted by: xicheng1 Posted on: 2006-05-19 17:12 用无限循环试试 X等于多少 就for 几次 |
4.Re:如何生成x位A-Z的字串呢? [Re: binge] | Copy to clipboard |
Posted by: Jcat Posted on: 2006-05-21 00:11 Solution1 以下方法的功能: 带入 返回 "aaa" "aab" "aaz" "aba" "zzz" "aaaa" public String AddOne(String s) { 相信有了这个方法,继续实现你的想法就不难了,剩下的自己搞定吧。 |
5.Re:如何生成x位A-Z的字串呢? [Re: binge] | Copy to clipboard |
Posted by: Jcat Posted on: 2006-05-21 00:26 Solution2 比较另类的办法,利用36进制数(Java的Integer类中的最高进制数)的toString方法。 x=2的实现 public void soCoolAA(int loopNumber) throws Exception{ 调用一下 soCool(676) 看看结果吧。 x=3的实现 public static void soCoolAAA(int loopNumber) throws Exception { 依此类推吧,不过这个办法效率低下,仅供参考。 |
6.Re:如何生成x位A-Z的字串呢? [Re: binge] | Copy to clipboard |
Posted by: oujianqiang Posted on: 2006-05-22 17:53 基础编程题而已,看一下这个程序吧
{ use [ code ] and check "Disable Smileys" -- why } |
7.Re:如何生成x位A-Z的字串呢? [Re: binge] | Copy to clipboard |
Posted by: binge Posted on: 2006-05-23 19:31 建议在贴切代码时加入 [ code ] public class A2Z(){ System.out.println("test code reference"); } [/ c"+"o"+"d"+"e ] |
8.Re:如何生成x位A-Z的字串呢? [Re: binge] | Copy to clipboard |
Posted by: chance2000 Posted on: 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); } 这个不知道是不是你要的结果?这个效率应该还算可以。 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |