zoukankan      html  css  js  c++  java
  • 回溯(排列组合)

     1 #include "iostream"
     2 #include "vector"
     3 using namespace std;
     4 
     5 void print(vector<int>Array)
     6 {
     7     int i;
     8     for(i=0;i<Array.size();i++)
     9     {
    10         cout<<Array[i]<<" ";
    11     }
    12     cout<<endl;
    13 }
    14 
    15 void swap(vector<int>&Array,int i,int j)
    16 {
    17     int temp;
    18     temp = Array[i];
    19     Array[i] = Array[j];
    20     Array[j] = temp;
    21 }
    22 
    23 void backtrack(int i,vector<int>Array)
    24 {
    25     if(i>=Array.size())
    26     {
    27         print(Array);
    28     }
    29     for(int j=i;j<Array.size();j++)
    30     {
    31         swap(Array,i,j);
    32         backtrack(i+1,Array);
    33         swap(Array,i,j);
    34     }
    35 }
    36 
    37 int main()
    38 {
    39     vector<int>Array;
    40     int N,i;
    41     cout<<"please input num N:N=";
    42     cin>>N;
    43     for(i=0;i<N;i++)
    44     {
    45         Array.push_back(i+1);
    46     }
    47     backtrack(0,Array);
    48 
    49     return 0;
    50 }

  • 相关阅读:
    EditText之边框颜色
    Android之drawable state各个属性详解
    Android-第三天
    Android 问题
    ios VS android
    Android-第二天(2)
    collapse
    2020/3/7
    2020/3/6
    P3825 [NOI2017]游戏
  • 原文地址:https://www.cnblogs.com/minmsy/p/5035442.html
Copyright © 2011-2022 走看看