zoukankan      html  css  js  c++  java
  • 回溯2--部分全排列

    回溯2--部分全排列

    一、心得

    二、题目及分析

    设有n个整数的集合{1,2,...,n},从中任意取出r个数进行排列(r<n),试着列出所有排列

    全排列的阉割版,修改输出限制条件即可

    三、代码及结果

     1 /*
     2 设有n个整数的集合{1,2,...,n},从中任意取出r个数进行排列(r<n),试着列出所有排列
     3 
     4 全排列的阉割版,修改输出限制条件即可
     5  
     6 */ 
     7 #include <iostream>
     8 using namespace std;
     9 //三个数组
    10 bool vis[100];
    11 int total=0;
    12 int ans[100];
    13 
    14 //输出结果
    15 void print(int r){
    16     if(total==5) return ;
    17     total++;
    18     cout<<"<"<<total<<">"<<endl;
    19     for(int i=1;i<=r;i++){
    20         cout<<ans[i]<<" ";
    21     }
    22     cout<<endl;
    23 } 
    24 
    25 void search(int t,int n,int r){
    26     if(t==r+1) print(r);
    27     for(int i=1;i<=n;i++){
    28         if(!vis[i]){
    29             ans[t]=i,vis[i]=1;
    30             search(t+1,n,r);
    31             vis[i]=0;
    32         }
    33     } 
    34 } 
    35 
    36 int main(){
    37     int n,r;
    38     cin>>n>>r;
    39     search(1,n,r);
    40     cout<<total<<endl;
    41     return 0;
    42 }

  • 相关阅读:
    C语言程序设计第一次作业
    C语言I博客作业09
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业05
    C语言II博客作业03
    C语言II博客作业02
    C语言II博客作业01
    学期总结
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/7119171.html
Copyright © 2011-2022 走看看