- 网易:题意很简单,写一个程序,打印出以下的序列。
(a),(b),(c),(d),(e)........(z)
(a,b),(a,c),(a,d),(a,e)......(a,z),(b,c),(b,d).....(b,z),(c,d).....(y,z)
(a,b,c),(a,b,d)....(a,b,z),(a,c,d)....(x,y,z)
....
(a,b,c,d,.....x,y,z)(思路:全组合问题)
#include "stdafx.h"
#include <iostream>
using namespace std;
#define TABLE_LEN 26
char a_table[TABLE_LEN];
char pstr[TABLE_LEN];
int current = 0;
bool pick_array(char *table, int pick_len, int len)
{
if (table == NULL)
return false;
else if(pick_len == 0 || len == 0){
pstr[current] = '\0';
printf("(%s),", pstr);
return true;
}
for (int i = 0; i < len - pick_len + 1; i++){
pstr[current++] = table[i];
if (!pick_array(table + i + 1, pick_len - 1, len - i - 1))
return false;
current--;
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < TABLE_LEN; i++){
a_table[i] = 'a' + i;
}
for (int len = 1; len <= TABLE_LEN; len++){
current = 0;
pstr[current] = '\0';
if (!pick_array(a_table, len, TABLE_LEN)){
printf("error\n");
return -1;
}
printf("\n");
}
std::cin.get();
return 0;
}