0分求助(悬关)

B3623 枚举排列(递归实现排列型枚举)

Fishen @ 2024-04-13 20:27:58

样例已过,求调。

#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[100],use[100];
void dfs(int pos){
    if(pos==n){
        for(int i=1;i<=k;i++)cout<<a[i]<<' ';
        cout<<endl;
        return;
    }
    for(int i=1;i<=n;i++){
        if(!use[i]){ 
            use[i]=1; 
            a[pos]=i; 
            dfs(pos+1); 
            use[i]=0; 
        }
    }
}
int main() {
    cin>>n>>k;
    dfs(1);
    return 0;
}

by Dream_Creator @ 2024-04-13 20:33:08

@Fishen


#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[1005],use[1005];
void dfs(int pos){
    if(pos==k+1){
        for(int i=1;i<=k;i++)cout<<a[i]<<' ';
        cout<<endl;
        return;
    }
    for(int i=1;i<=n;i++){
        if(!use[i]){ 
            use[i]=1; 
            a[pos]=i; 
            dfs(pos+1); 
            use[i]=0; 
        }
    }
}
int main() {
    cin>>n>>k;
    dfs(1);
    return 0;
}

by queenbee @ 2024-04-13 20:35:44

@Fishen pos==k+1


by Fishen @ 2024-04-13 20:46:22

@Dream_Creator ,@queenbee 感谢大佬,已关


|