输入一组数字,求数字的全序列
如何实现数字的全序列?求教
2015-03-25 22:19
程序代码:
#include <stdio.h>
#include <string.h>
#define SWAP(x, y, t) ((t) = (x), (x) = (y), (y) = (t))
void foo(char* s, int from, int to);
int main(void) {
char num[5];
scanf("%5s", num);
foo(num, 0, strlen(num) - 1);
putchar('\n');
return 0;
}
void foo(char *s, int from, int to) {
int i, j, t;
if(to <= 1) return;
if(from == to) {
for(i = 0; i <= to; i++) putchar(s[i]);
putchar('\t');
} else {
for(j = from; j <= to; j++) {
SWAP(s[j], s[from], t);
foo(s, from + 1, to);
SWAP(s[j], s[from], t);
}
}
}

2015-03-25 22:59