public class AnagarmApp {
static int size;
static int count;
static char[] arrChar = new char[100];
public static void main(String[] args) {
String input = "cat";
size = input.length();
count = 0;
for (int j = 0; j < size; j++){
arrChar[j] = input.charAt(j);
}
doAnagram(size);
}
/**
* 利用遞歸,循環替換字母的位置
* @param newSize 遞歸執行後的新參數
*/
public static void doAnagram(int newSize){
if (newSize == 1){
return;
}
for (int j = 0; j < newSize; j++){
doAnagram(newSize - 1);
//如果參數爲2 則輸出。
if (newSize == 2){
displayWord();
}
//換位方法
rotate(newSize);
}
}
/**
* 將數組中的字母移動,n-1 = n
*
* @param newSize 遞歸後的數組大小參數
*/
public static void rotate(int newSize){
int j;
//獲取起始位置
int position = size - newSize;
//獲取起始位置的值
char temp = arrChar[position];
//移動
for (j = position + 1; j < size; j++){
arrChar[j-1] = arrChar[j];
}
arrChar[j-1] = temp;
}
public static void displayWord(){
System.out.print(++count + " ");
for (int j = 0; j < size; j++){
System.out.print(arrChar[j]);
}
}
}