C离散数学-组合-结构数学-选择三种限制条件下的排列问题

360影视 动漫周边 2025-04-03 21:33 2

摘要:用Python语言程序表述的分子式解析与集合操作。Python不同材料元素集合合并为总集合分析复合材料所有组成元素-化学中的应用。等等。

离散量与结构数学-组合数学计算机处理实例

组合数学应用

用Python语言程序表述的分子式解析与集合操作。Python不同材料元素集合合并为总集合分析复合材料所有组成元素-化学中的应用。等等。

C语言由于它的自身严谨-硬件操作与数值计算问题的根性应用,包括各种C编译器也就非常严谨。所以有了下面程序的第一行代码。

#pragma warning(disable:4996)

///////////////////////

#include

#include

#include

#define MAX_ELEMENTS 100

#define MAX_LENGTH 100

void swap(char* a, char* b) {

char temp = *a;

*a = *b;

*b = temp;

}

void permute_without_duplicates(char* elements, int start, int end) {

if (start == end) {

printf("%s\n", elements);

}

else {

for (int i = start; i

int is_duplicate = 0;

for (int j = start; j

if (elements[j] == elements[i]) {

is_duplicate = 1;

break;

}

}

if (is_duplicate) continue;

swap(&elements[start], &elements[i]);

permute_without_duplicates(elements, start + 1, end);

} } }

int compare(const void* a, const void* b) {

return strcmp(*(const char**)a, *(const char**)b);

}

void generate_permutations(char* elements, int start, int end, char*** permutations, int* count, int* capacity) {

if (start == end) {

if (*count >= *capacity) {

*capacity *= 2;

*permutations = realloc(*permutations, (*capacity) * sizeof(char*));

if (!*permutations) {

printf("内存分配失败\n");

exit(1);

} }

(*permutations)[*count] = malloc((end - start + 2) * sizeof(char));

if (!(*permutations)[*count]) {

printf("内存分配失败\n");

exit(1);

}

strcpy((*permutations)[*count], elements);

(*count)++;

}

else {

for (int i = start; i

swap(&elements[start], &elements[i]);

generate_permutations(elements, start + 1, end, permutations, count, capacity);

} } }

void permute_with_duplicates(char* elements, int start, int end) {

int capacity = 10;

int count = 0;

char** permutations = malloc(capacity * sizeof(char*));

if (!permutations) {

printf("内存分配失败\n");

exit(1);

}

generate_permutations(elements, start, end, &permutations, &count, &capacity);

qsort(permutations, count, sizeof(char*), compare);

for (int i = 0; i

if (i == 0 || strcmp(permutations[i], permutations[i - 1]) != 0) {

printf("%s\n", permutations[i]);

}

free(permutations[i]);

}

free(permutations);

}

void permute_with_length(char* elements, int len, int current_length,

char* current_permutation, int used) {

if (current_length == len) {

current_permutation[current_length] = '\0';

printf("%s\n", current_permutation);

return;

}

for (int i = 0; elements[i] != '\0'; i++) {

if (!used[i]) {

if (current_length >= MAX_LENGTH - 1) {

printf("超出最大长度限制(%d)", MAX_LENGTH);

return;

}

used[i] = 1;

current_permutation[current_length] = elements[i];

permute_with_length(elements, len, current_length + 1, current_permutation, used);

used[i] = 0;

} } }

int main {

int choice;

char elements[MAX_ELEMENTS] = { 0 };

int length;

printf("请选择限制条件:\n");

printf("1. 无重复元素的全排列\n");

printf("2. 有重复元素的全排列\n");

printf("3. 指定长度的排列\n");

printf("请输入选择的序号 (1/2/3): ");

if (scanf_s("%d", &choice) != 1 || choice 3) {

printf("输入无效,请输入有效的数字(1/2/3)。\n");

return 1;

}

getchar;

printf("请输入元素,用逗号分隔: ");

if (fgets(elements, MAX_ELEMENTS, stdin) == NULL) {

printf("输入无效\n");

return 1;

}

int j = 0;

for (int i = 0; elements[i] != '\0' && j

if (elements[i] == ',' || elements[i] == '\n' || elements[i] == ' ') {

continue;

}

elements[j++] = elements[i];

}

elements[j] = '\0';

if (strlen(elements) == 0) {

printf("未输入有效元素\n");

return 1;

}

switch (choice) {

case 1:

permute_without_duplicates(elements, 0, strlen(elements) - 1);

break;

case 2:

permute_with_duplicates(elements, 0, strlen(elements) - 1);

break;

case 3: {

printf("请输入排列长度: ");

if (scanf_s("%d", &length) != 1 || length strlen(elements)) {

printf("输入无效,请输入1-%d之间的数字。\n", strlen(elements));

return 1;

}

getchar;

char current_permutation[MAX_LENGTH] = { 0 };

int used[MAX_ELEMENTS] = { 0 };

permute_with_length(elements, length, 0, current_permutation, used);

break;

}

default:

printf("无效选择\n");

}

return 0;

}

来源:三日雨-等风来

相关推荐