import java.io.*; class AnagramApp { static int size; static char[] arrChar; public static void main(String[] args) throws IOException { System.out.print("Enter a word: "); String input = getString(); size = input.length(); arrChar = new char[size]; for (int j = 0; j < size; j++) arrChar[j] = input.charAt(j); doAnagram(size); } public static void doAnagram(int newSize) { if (newSize==1)return; for(int i=0;i<newSize;++i){ doAnagram(newSize-1); if (newSize==2) displayWord(); rotate(newSize); } } public static void rotate(int newSize) { int positon=size-newSize; char temp=arrChar[positon]; int j; for (j=positon+1;j<size;++j) arrChar[j-1]=arrChar[j]; arrChar[j-1]=temp; } public static void displayWord() { for (int j = 0; j < size; j++) System.out.print(arrChar[j]); System.out.print(" "); System.out.flush(); } public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } }
for(int i=0;i<newSize;++i)
if (newSize==2)为什么用了它就不会重复显示
这两段是如何想出来的
怳然如夢
相关分类