题解 | #全排列#
全排列
https://www.nowcoder.com/practice/5632c23d0d654aecbc9315d1720421c1
bug:第21行为res[u] = str[i];原来写的是res[i] = str[i];
另解:可用next_permutation解决
#include<iostream>
#include<cstring>
using namespace std;
const int N = 30;
string str;
char res[N];
bool st[N];
void f(int u){
int len = str.size();
if(u == len){
cout<<res<<endl;
return;
}
for(int i = 0;i < len;i++){
if(st[i] != 1){
res[u] = str[i];
st[i] = 1;
f(u + 1);
st[i] = 0;//回溯
}
}
}
int main(){
cin>>str;
f(0);
return 0;
}
王道考研机试 文章被收录于专栏
包含考研机试打卡表题目
查看7道真题和解析