Java递归算法——变位字
轮换的含义
1.c ats --> 2.ca st
3.c tsa --> 4.ct as
5.c sat --> 6.cs ta
7. atsc
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Anagram_demo { static int Size; static int count=0; static char[] arrChar = new char[100]; public static void main(String[] args) throws Exception{ // TODO 自动生成的方法存根 System.out.println("请输入一个单词:"); String input = getString(); Size = input.length(); for(int i=0;i<Size;i++){ arrChar[i] = input.charAt(i); } 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(); } rorate(newSize); } } public static void displayWord(){ System.out.print(++count); for(int i=0;i<Size;i++){ System.out.print(arrChar[i]); } System.out.println("、"); } public static void rorate(int newSize){ //轮换函数 int position = Size - newSize; char temp = arrChar[position]; for(int i=position;i<Size-1;i++){ arrChar[i] = arrChar[i+1]; } arrChar[Size-1] = temp; } //输出方法 public static String getString() throws IOException{ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } //输出方法 public static int getInt() throws IOException{ String s = getString(); return Integer.parseInt(s); } }
本文只发表于博客园和tonglin0325的博客,作者:tonglin0325,转载请注明原文链接:https://www.cnblogs.com/tonglin0325/p/5360034.html