C 程序来查找给定字符串的排列
假设我们在一个数组中有几个字符串。我们必须在不同的行中找到它们的所有排列。
因此,如果输入类似于strings=["abc","def","ghi"],那么输出将是
abc def ghi abc ghi def def abc ghi def ghi abc ghi abc def ghi def abc
示例
让我们看看以下实现以获得更好的理解-
#include <stdio.h>
#include <string.h>
int next_permutation(int n, char **s){
for (int i = n - 1; i > 0; i--)
if (strcmp(s[i], s[i - 1]) > 0){
int j = i + 1;
for (; j < n; j++)
if (strcmp(s[j], s[i - 1]) <= 0)
break;
char *t = s[i - 1];
s[i - 1] = s[j - 1];
s[j - 1] = t;
for (; i < n - 1; i++, n--){
t = s[i];
s[i] = s[n - 1];
s[n - 1] = t;
}
return 1;
}
for (int i = 0; i < n - 1; i++, n--){
char *t = s[i];
s[i] = s[n - 1];
s[n - 1] = t;
}
return 0;
}
int main(){
char *strings[] = {"abc", "def", "ghi"};
int n = 3;
do{
for (int i = 0; i < n; i++)
printf("%s%c", strings[i], i == n - 1 ? '\n' : ' ');
} while (next_permutation(n, strings));
}输入
{"abc", "def", "ghi"}输出结果abc def ghi abc ghi def def abc ghi def ghi abc ghi abc def ghi def abc
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短