• java学习之—递归实现变位字


    /**
     * 递归实现变位字
     * Create by Administrator
     * 2018/6/20 0020
     * 上午 10:23
     **/
    public class AnagramApp {
    
        static int size;
        static int count;
        static char[] arrChar = new char[100];
    
        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 i;
            int position = size - newSize;
            char temp = arrChar[position];
            for (i=position+1; i < size; i++) {
                arrChar[i-1] = arrChar[i];
            }
            arrChar[i-1] = temp;
        }
    
        public static void displayWord(){
            if(count < 99){
                System.out.print(" ");
            }else if(count < 9){
                System.out.print(" ");
            }
            System.out.print(++count + " ");
            for (int i = 0; i < size; i++) {
                System.out.print(arrChar[i]);
            }
            System.out.print("  ");
            System.out.flush();
            if(count%6 == 0){
                System.out.println("");
            }
        }
    
        private static String getString() throws IOException {
            InputStreamReader inputStreamReader = new InputStreamReader(System.in);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            return bufferedReader.readLine();
        }
    
        public static void main(String[] args) throws IOException{
    
            System.out.println("Enter a word:");
            String input = getString();
            size = input.length();
            count = 0;
            for (int i = 0; i < size; i++){
                arrChar[i] = input.charAt(i);
            }
            doAnagram(size);
        }
    

      

  • 相关阅读:
    让textarea完全显示文章并且不滚动、不可拖拽、不可编辑
    解决css3毛玻璃效果(blur)有白边问题
    mysql_binlog恢复
    SED_AWK_正则
    进程;线程
    网络编程
    面向对象
    python_递归_协程函数(yield关键字)_匿名函数_模块
    Python 函数对象 命名空间与作用域 闭包函数 装饰器 迭代器 内置函数
    python_字符_函数
  • 原文地址:https://www.cnblogs.com/chancy/p/9202772.html
Copyright © 2020-2023  润新知