写一个程序,打印出以下的序列。
(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)。
#include <stdio.h>
#include <stdlib.h>
#define SIZE 8
//生成字母表
void createAphalbit(char* aphal) {
int i = 0;
for (; i < SIZE; i++) {
aphal[i] = i + 'a';
}
aphal[i] = '\0';
}
void calcPemutation(char* aphal, char* result, int reach, int len) {
if (len == reach) {
result[reach] = '\0';
int i = 0;
int printable = 1;
//去除降序或等序序列
for (; i + 1 < len; i++) {
if (result[i] >= result[i + 1]) {
printable = 0;
break;
}
}
if (printable) {
int i = 0;
printf("(");
printf("%c", result[i++]);
for (; i < len; i++) {
printf(",%c", result[i]);
}
printf(")");
}
} else {
int i = 0;
for (; i < SIZE; i++) {
//结果拾取
result[reach] = aphal[i];
calcPemutation(aphal, result, reach + 1, len);
}
}
}
//指定结果集长度下调用
void ctrlPos(char* str, int len) {
int i = 0;
char result[len + 1];
// for (; i < len; i++) {
calcPemutation(str, result, i, len);
// }
}
//长度控制
void ctrlLength(char* str) {
int i = 1;
for (; i <= SIZE; i++) {
ctrlPos(str, i);
printf("\n");
}
}
int main(void) {
char aphalbit[SIZE + 1];
createAphalbit(aphalbit);
ctrlLength(aphalbit);
return EXIT_SUCCESS;
}