zoukankan      html  css  js  c++  java
  • 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;
    
    //=================================================
    // File Name       :	Anagram_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    //类名:BinarySearch_Find
    //属性:
    //方法:
    
    
    
    //主类
    //Function        : 	Anagram_demo
    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);
    				
    			}
    
    }
    
  • 相关阅读:
    Light oj 1082 Array Queries(区间最小值)
    Codeforces Round #179 (Div. 2)A、B、C、D
    poj 1976 A Mini Locomotive(01背包)
    Codeforces Round #178 (Div. 2)
    hackerrank challenges median
    poj 1961 Period(kmp最短循环节)
    poj 2182 Lost Cows(树状数组)
    ZOJ1117 POJ1521 HDU1053 Huffman编码
    poj 2352 Stars 树状数组
    这可能是最适合萌新入门Web安全的路线规划
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5360034.html
Copyright © 2011-2022 走看看