组合问题(递归)

//============================================================================
// Name : ForJob.cpp
// Author : yangyh
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
//从M个字符中选出N个字符的所有组合
#include <iostream>
using namespace std;

static int m = 0;
int print(const char * src, int begin, int len, char *dst, int index, int n) {

if (n == 0) {
dst[index]
= '\0';
printf(
"%d:%s\n", m++, dst);
return 1;
}
if (len < n)
return 0;
for (int i = begin; i < len; i++) {

dst[index]
= src[i];
print(src, i
+ 1, len, dst, index + 1, n - 1);

}
return 1;
}
int main(int argc, char* argv[]) {
printf(
"Hello World!\n");
char str[10] = "abcdefgh";
char dst[10];
print(str,
0, 8, dst, 0, 3);//从8个字符中找出3个字符的所有组合
printf("total:%d\n", m);
return 0;
}

posted on 2011-08-10 01:04  yangyh  阅读(458)  评论(0编辑  收藏  举报