06_递归

佐手、 提交于 2020-03-06 01:20:46

样例输入:

3 4

样例输出:

4 3 2
4 3 1
4 2 1
3 2 1

AC代码

#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;

int m,n;
vector<int>hashtable;
vector<int>ans;
void toarray(int index)
{
    if(index==3)
    {
        for(int i=0;i<3;i++)
        {
            printf("%d ",ans[i]);
        }
        printf("\n");
        return ;
    }
    for(int i=n;i>=1;i--)
    {
        if(hashtable[i]==0&&(ans.size()==0||i<ans[ans.size()-1]))
        {
            hashtable[i]=1;
            ans.push_back(i);
            toarray(index+1);
            ans.pop_back();
            hashtable[i]=0;
        }
    }
}


int main()
{
    scanf("%d%d",&m,&n);
    hashtable.resize(n+1);
    fill(hashtable.begin(),hashtable.end(),0);
    toarray(0);
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!