C++用回溯方法做全排列的代码
学习闲暇时间,将内容过程经常用的一些内容记录起来,下边内容是关于C++用回溯方法做全排列的内容,应该能对各位有一些好处。
#include<cstring>#include<iostream>#define LEN 10using namespace std;char elem[LEN] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };char result[LEN];bool filled[LEN];void permutation(int k, int n) { if (k == n) { for (int i = 0; i < n; i++) { cout << result[i] << " "; } cout << endl; } else { for (int i = 0; i < n; i++) { if (!filled[i]) { filled[i] = true; result[k] = elem[i]; permutation(k + 1, n); filled[i] = false; } } }}int main() { memset(result, 0, sizeof(result)); memset(filled, false, sizeof(filled)); permutation(0, LEN); return 0;}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。