c语言字典序全排列代码
时间: 2023-12-19 19:27:59 浏览: 126
C语言排序代码
5星 · 资源好评率100%
以下是C语言的字典序全排列代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(char *a, char *b) {
char temp = *a;
*a = *b;
*b = temp;
}
void reverse(char *begin, char *end) {
while (begin < end) {
swap(begin, end);
begin++;
end--;
}
}
int next_permutation(char *str, int len) {
int i = len - 2;
while (i >= 0 && str[i] >= str[i + 1]) {
i--;
}
if (i < 0) {
return 0;
}
int j = len - 1;
while (j > i && str[j] <= str[i]) {
j--;
}
swap(&str[i], &str[j]);
reverse(&str[i + 1], &str[len - 1]);
return 1;
}
int main() {
char str[] = "abc";
int len = strlen(str);
do {
printf("%s\n", str);
} while (next_permutation(str, len));
return 0;
}
```
阅读全文