日期:2014-05-19 浏览次数:20667 次
public class Test { public static void main(String[] args) throws Throwable { String s = "login".toLowerCase(); char[] c = s.toCharArray(); int[] idx = new int[c.length]; StringBuilder buf = new StringBuilder(); while (idx[0] < 2) { buf.delete(0, buf.length()); for (int i=0; i<idx.length; i++) { if (idx[i] == 1) { buf.append(Character.toUpperCase(c[i])); } else { buf.append(c[i]); } } System.out.println(buf); idx[idx.length-1]++; for (int i=idx.length-1; i>0; i--) { if (idx[i] == 2) { idx[i] = 0; idx[i-1]++; } else { break; } } } } }
------解决方案--------------------
public class Test26 { public static void main(String[] args) { String str = "lOgiN"; String lowStr = str.toLowerCase(); int length = lowStr.length(); for(int i=0; i<(int)Math.pow(2, lowStr.length());i++){ char[] ch = lowStr.toCharArray(); String s = Integer.toBinaryString(i); for(int j=0; j<s.length(); j++){ if('1' == s.charAt(s.length() -j -1)) { ch[length - j -1] = (char)(ch[length - j -1] - 32); } } System.out.println(new String(ch)); } } }