zoukankan      html  css  js  c++  java
  • 全排列

    package com.perm;
    
    public class Permutation {
    
        public static void perm(int[] num, int i) {
    
            if (i < num.length - 1) {
    
                for (int j = i; j <= num.length - 1; j++) {
                    int temp = num[j];
    
                    // 旋转该区间最右边数字至最左边
                    for (int k = j; k > i; k--) {
                        num[k] = num[k - 1];// 减法
                    }
    
                    num[i] = temp;
                    perm(num, i + 1);
    
                    // 还原
                    for (int k = i; k < j; k++) {
                        num[k] = num[k + 1];// 加法
                    }
    
                    num[j] = temp;
                }
            } else {
    
                // 显示此次排列
                for (int j = 1; j <= num.length - 1; j++) {
                    System.out.print(num[j] + " ");
                }
                System.out.println();
            }
        }
    
        public static void main(String[] args) {
            int[] num = new int[4 + 1];
    
            for (int i = 1; i <= num.length - 1; i++) {
                num[i] = i;
            }
    
            perm(num, 1);
        }
    }
  • 相关阅读:
    $.getJSON()
    seconds
    ini_set
    validation
    component
    ini_set();
    长期阅读英文技术博客的好处
    用xml还是json
    单​手​打​字
    洛谷P1141 01迷宫
  • 原文地址:https://www.cnblogs.com/missliuxin/p/4168045.html
Copyright © 2011-2022 走看看