tonglin0325的个人主页

Java递归算法——变位字

轮换的含义

1.c ats –> 2.ca st

3.c tsa –> 4.ct as

5.c sat –> 6.cs ta

  1. atsc

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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);

}
}