题解 | #全排列#

全排列

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;
}

王道考研机试 文章被收录于专栏

包含考研机试打卡表题目

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务