校招笔试中组合常考题

/*
* 产生“abc”所有的可能组合。abc,acb,bac,bca,cba,cab
*/
#include <iostream>
#include <vector>
using namespace std;
bool isswap(char *pb, char *pe){
	char *p;
	for (p = pb; p < pe; ++p){
		if (*p == *pe) return false;
	}
	return true;
}
void permutation(char* ps, char* pb){
	if (!ps) return;
	if (*pb == '\0'){
		static int num = 1;
		printf("%s\n", ps);
	}
	else{
		for (char* pc = pb; *pc != '\0'; ++pc){
			if (isswap(pb, pc)){
				swap(*pb, *pc);
				permutation(ps, pb + 1);
				swap(*pb, *pc);
			}
		}
	}
}
int main(){
	char s[]= "abc";
	permutation(s, s);
	while (1);
	return 0;
}

你可能感兴趣的:(排列组合)