• Algorithm1: 全排列


    全排列
    思想:
         这是一个全排列问题,需要使用递归实现,将数组中的所有元素和第一个元素交换,求后面n-1个元素的全排列。
         按照这个条件递归下去,知道元素的个数只有一个的时候,输出所有的元素。
     
    #include <iostream>
    using namespace std;
     
    int total = 0;
    void perm(int arr[], int start , int size){
                     if (start >= size){
                                     for (int i = 0; i < size; i++){
                                                    cout << arr[i] << " " ;
                                    }
                                    cout << endl;
                                    total++;
                                     //return;
                    }
                     else{
                                     for (int i = start; i < size; i++){
                                                     int temp = arr [start];
                                                     arr[start ] = arr[i];
                                                     arr[i] = temp;
     
                                                    perm( arr, start + 1, size);
     
                                                    temp = arr[start ];
                                                     arr[start ] = arr[i];
                                                     arr[i] = temp;
                                    }
                    }
    }
     
    int main(){
                     int a [] = { 1, 2, 3, 4, 5, 6 };
                    perm(a, 0, 3);
                    cout<< total << endl;
                     return 0;
    }
     
    此外在C++STL中也有一个关于全排列的函数:
         next_permuntation(start, start_length);
         int a [] = { 1, 2, 3, 4, 5, 6 };
                    sort(a, a + 6);
                     while (next_permutation(a, a + 6)){
                                     for (int i = 0; i < 6; i++){
                                                    cout << a[i] << " " ;
                                    }
                                    cout << endl;
                    }
  • 相关阅读:
    1.ok6410移植bootloader,移植u-boot,学习u-boot命令
    ok6410按键中断编程,linux按键裸机
    四. jenkins部署springboot项目(1)--window环境
    一.jenkins安装(windows环境)
    oracle服务端导出/导入方式expdp/impdp
    linux 日志文件查看
    linux kafka进程挂了 自动重启
    kafka manager遇到的一些问题
    if条件语句
    shell脚本的条件测试与比较
  • 原文地址:https://www.cnblogs.com/hbhzsysutengfei/p/3414568.html
Copyright © 2020-2023  润新知