package com.itheima;
/**
* 编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符, 例如:原始字符串是"abc", 打印得到下列所有组合情况: "a" "b" "c"
* "ab" "bc" "ca" "ba" "cb" "ac" "abc" "acb" "bac" "bca" "cab" "cba"
*
* @author Administrator
*
*/
public class Test8 {
public static void main(String[] args) {
char[] s = { 'a', 'b', 'c' };
int[] n = { 1, 2, 3 };
// 单个字符
for (int i = 0; i < s.length; i++) {
System.out.println(s[i]);
}
// 两个字符
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
if (i != j) {
System.out.print(s[i]);
System.out.println(s[j % 3]);
}
}
// 三个字符
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
for (int k = 0; k < 3; k++) {
if (i != j && j != k && k != i) {
System.out.print(s[i]);
System.out.print(s[j % 3]);
System.out.println(s[3 - i - (j % 3)]);
}
}
}
}
|