像素


私信TA

用户名:dotcpp0735122

访问量:251

签 名:

落山

等  级
排  名 9657
经  验 1079
参赛次数 0
文章发表 12
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

TA的其他文章

解题思路:1~n选择的排列数量r,递归可以用两个变量一个用于控制走,一个控制数量


注意事项:采用的是框架写的:

#include <bits/stdc++.h>

using namespace std;


#define endl '\n'

#define int long long


signed main(){

ios::sync_with_stdio(0);

cin.tie(0);

cout.tie(0);

return 0;


参考代码:

#include <bits/stdc++.h>

using namespace std;


#define endl '\n'

#define int long long

const int N=50;

int a[N];

bool v[N];

int n,r;


void dfs(int x,int t){

    if(t==r){

        for(int i=0;i<n;i++){

            if(v[i]){

               if(a[i]<10){

                   cout<<"  "<<a[i];

               }

               else{

                   cout<<" "<<a[i];

               }

            }

        }

        cout<<endl;

    }

    

    for(int i=x;i<n;i++){

        if(!v[i]){

            v[i]=true;

            dfs(i,t+1);

            v[i]=false;

        }

    }

}


signed main(){

ios::sync_with_stdio(0);

cin.tie(0);

cout.tie(0);

cin>>n>>r;

for(int i=0;i<n;i++){

    a[i]=i+1;

}

dfs(0,0);

return 0;


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区