转位字算法实现

来源:互联网 发布:gt610数据 编辑:程序博客网 时间:2024/05/29 09:15

package arithmetic;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Anagram
{
    static int size;
    static int count;
    static char[] arrChar=new char[200];
   
    public static void doAnagram(int  newSize){
        if(newSize==0){
            return;
        }
        for (int i = 0; i < newSize; i++)
        {
            doAnagram(newSize-1);
            if(newSize==2){
              displayWord(); 
            }
            rotate(newSize);
        }
    }

    private static void rotate(int newSize)
    {
        int j;
        int positiogn=size - newSize;
        char temp=arrChar[positiogn];
        for (j= positiogn+1; j < size; j++)
        {
            arrChar[j-1]=arrChar[j];
        }
        arrChar[j-1]=temp;      
    }

    private static void displayWord()
    {
        if(count < 99)
            System.out.print(" ");
            if(count < 9)
            System.out.print(" ");
            System.out.print(++count + " ");
            for(int j=0; j<size; j++)
            System.out.print( arrChar[j] );
            System.out.print(" ");
            System.out.flush();
            if(count%6 == 0)
            System.out.println("");       
    }
   
    public static String getString() throws IOException
    {
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    String s = br.readLine();
    return s;
    }

    /** 主函数
     * @throws IOException
     */
    public static void main(String[] args) throws IOException
    {
        System.out.print("Enter a word: ");
        System.out.flush();
        String input = getString();
        size = input.length();
        count = 0;
        for(int j=0; j<size; j++)
        arrChar[j] = input.charAt(j);
        doAnagram(size);
        }
}