排列函数打印数组,但 returns 为空的数组
Permutation function print array but the array that returns is empty
我需要一些关于此函数的帮助,该函数根据输入中的数组指针进行排列。
这是代码。
#include <stdio.h>
#include <stdlib.h>
void input_array(int *arr, int size);
void print_array(int *arr, int size);
void array_to_matrix(int **matrix, int *arr, int row, int col);
void print_matrix(int **matrix, int row, int col);
int factorial(int n)
{
if (n == 0)
return 1;
else
return (n * factorial(n - 1));
}
void swap(int *x1, int *x2)
{
int x = *x1;
*x1 = *x2;
*x2 = x;
}
int *permute(int *new_arr, int st, int ls)
{
int i = 0;
if (st == ls) {
// int k;
// for (k = 0; k < ls; k++)
// {
// printf("%d ", new_arr[k]);
// }
// printf("\n");
return new_arr;
} else {
for (i = st; i < ls; i++)
{
swap(new_arr + st, new_arr + i);
permute(new_arr, st + 1, ls);
swap(new_arr + st, new_arr + i);
}
}
return new_arr;
}
int main()
{
int m, n, *arr, **mat;
int size, i;
//INIZIO CALCOLO MATRICE DI ARRAY INIZIALE (matrice dei costi)
//inserisci il numero di colonne, che corrisponde al numero di città
printf("Enter the number of columns(n):");
if ((scanf("%d", &n) != 1) || (n < 1)) {
puts("invalid value for columns");
return -1;
}
m = n;
//m=numero di righe n=numero di colonne
size = m * n;
if (((arr = malloc(size * sizeof(int))) == NULL) ||
((mat = malloc(m * sizeof(int))) == NULL)) {
puts("not enough memory");
exit(-1);
}
for (i = 0; i < m; ++i) {
if ((mat[i] = malloc(n * sizeof(int))) == NULL) {
puts("not enough memory");
exit(-1);
}
}
input_array(arr, size);
print_array(arr, size);
array_to_matrix(mat, arr, m, n);
print_matrix(mat, m, n);
for (i = 0; i < m; ++i)
free(mat[i]);
free(mat);
//INIZIO CALCOLO PERMUTAZIONI (matrice dei percorsi possibili)
int nrighe = factorial(n);
int *new_array;
int st = 0, k = 0, j;
if (((new_array = malloc((n * nrighe) * sizeof(int))) == NULL) ||
((mat = malloc((n * nrighe) * sizeof(int))) == NULL)) {
puts("not enough memory");
exit(-1);
}
for (i = 0; i < m; ++i) {
if ((mat[i] = malloc((n * nrighe) * sizeof(int))) == NULL) {
puts("not enough memory");
exit(-1);
}
}
for (j = 1; j < n + 1; j++) {
new_array[k] = j;
printf("newarray[%d", k);
printf("]= %d", j);
k++;
}
free(arr);
if ((arr = malloc((n * nrighe) * sizeof(int))) == NULL) {
puts("not enough memory");
exit(-1);
}
printf("\nPermutations of dimension: %d\n", n);
arr = permute(new_array, st, n);
k = 0;
for (k = 0; k < n; k++) {
printf("%d ", arr[k]);
}
printf("\n");
array_to_matrix(mat, new_array, nrighe, n);
print_matrix(mat, nrighe, n);
free(arr);
free(new_array);
return 0;
}
void input_array(int *arr, int size)
{
int i;
for (i = 0; i < size; i++) {
printf("Enter element a[%d]", i);
if (scanf("%d", &arr[i]) != 1) {
int c;
puts("invalid value, redo");
/* flush invalid value up to the end of line */
while ((c = getchar()) != '\n') {
if (c == EOF) {
puts("EOF, abort");
exit(-1);
}
}
i -= 1;
}
}
}
void print_array(int *arr, int size)
{
int i;
printf("\n 1D array is as follows : \n");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
void array_to_matrix(int **matrix, int *arr, int row, int col)
{
int i, j, k = 0;
for (i = 0; i < row; i++) {
for (j = 0;j < col; j++) {
matrix[i][j] = arr[k++];
}
}
}
void print_matrix(int **matrix, int row, int col)
{
int i, j;
printf("\n 2D matrix is as follows : \n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
当我像评论中的部分一样打印数组时,它起作用了。它打印它找到的所有排列。问题是当我尝试从 return 函数中打印它时,在 main()
函数中,我从 permute()
函数中获取数组 returned,然后使用 for
loop 我尝试打印它,但它说它是空的;循环不打印正确的值。我不明白为什么。有人可以帮我吗?
permute
会计算并且可以显示作为参数传递的数组的所有排列,但它不能 return 单独显示它们。如果您想将所有排列存储到一个矩阵中,您应该以这种方式将目标数组与索引一起传递:
int permute(int *arr, int st, int ls, int *new_array, int k)
{
int i;
if (st == ls) {
/* store a new permutation */
for (i = 0; i < ls; i++) {
new_array[k++] = arr[i];
}
} else {
for (i = st; i < ls; i++) {
swap(arr + st, arr + i);
k = permute(arr, st + 1, ls, new_array, k);
swap(arr + st, arr + i);
}
}
return k;
}
这是您的程序的修改版本,修复了更多错误并包括可以处理重复项的 permute
版本:
#include <stdio.h>
#include <stdlib.h>
void input_array(int *arr, int size);
void print_array(const int *arr, int size);
int **allocate_matrix(int rows, int cols);
void free_matrix(int **mat, int rows, int cols);
void array_to_matrix(int **matrix, const int *arr, int rows, int cols);
void print_matrix(int **matrix, int rows, int cols);
int factorial(int n) {
if (n <= 0)
return 1;
else
return n * factorial(n - 1);
}
void swap(int *x1, int *x2) {
int x = *x1;
*x1 = *x2;
*x2 = x;
}
int permute(int *arr, int st, int ls, int *dest, int k) {
if (st == ls) {
/* store a new permutation */
for (int i = 0; i < ls; i++) {
dest[k++] = arr[i];
}
} else {
k = permute(arr, st + 1, ls, dest, k);
for (int i = st + 1; i < ls; i++) {
/* eliminate permutations of identical elements */
if (arr[st] != arr[i]) {
swap(arr + st, arr + i);
k = permute(arr, st + 1, ls, dest, k);
swap(arr + st, arr + i);
}
}
}
return k;
}
int main() {
int n, nrighe, size, *arr, **mat;
//INIZIO CALCOLO MATRICE DI ARRAY INIZIALE (matrice dei costi)
//inserisci il numero di colonne, che corrisponde al numero di città
printf("Enter the number of columns(n): ");
if (scanf("%d", &n) != 1 || n < 1) {
fprintf(stderr, "invalid value for columns\n");
exit(1);
}
#if 1
nrighe = n;
//nrighe = number of rows, n = number of columns
size = nrighe * n;
if (((arr = calloc(size, sizeof(*arr))) == NULL)
|| ((mat = allocate_matrix(nrighe, n)) == NULL)) {
fprintf(stderr, "not enough memory\n");
exit(1);
}
input_array(arr, size);
print_array(arr, size);
array_to_matrix(mat, arr, nrighe, n);
print_matrix(mat, nrighe, n);
free_matrix(mat, nrighe, n);
#endif
//INIZIO CALCOLO PERMUTAZIONI (matrice dei percorsi possibili)
nrighe = factorial(n);
size = nrighe * n;
if (((arr = calloc(size, sizeof(*arr))) == NULL)
|| ((mat = allocate_matrix(nrighe, n)) == NULL)) {
fprintf(stderr, "not enough memory\n");
exit(1);
}
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
printf("newarray[%d] = %d\n", i, i + 1);
}
printf("\nPermutations of dimension: %d\n", n);
permute(arr, 0, n, arr, 0);
array_to_matrix(mat, arr, nrighe, n);
print_matrix(mat, nrighe, n);
free(arr);
free_matrix(mat, nrighe, n);
return 0;
}
void input_array(int *arr, int size) {
for (int i = 0; i < size; i++) {
printf("Enter element a[%d]: ", i);
if (scanf("%d", &arr[i]) != 1) {
int c;
fprintf(stderr, "invalid value, redo\n");
/* flush invalid value up to the end of line */
while ((c = getchar()) != '\n') {
if (c == EOF) {
fprintf(stderr, "EOF, abort\n");
exit(1);
}
}
i -= 1;
}
}
}
void print_array(const int *arr, int size) {
printf("\n 1D array is as follows:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
/* allocate a matrix of m rows and n columns initialized to 0 */
int **allocate_matrix(int rows, int cols) {
int **mat;
if ((mat = calloc(rows, sizeof(*mat))) == NULL)
return NULL;
for (int i = 0; i < rows; ++i) {
if ((mat[i] = calloc(cols, sizeof(*mat[i]))) == NULL) {
while (i-- > 0)
free(mat[i]);
free(mat);
return NULL;
}
}
return mat;
}
void free_matrix(int **mat, int rows, int cols) {
if (mat) {
for (int i = 0; i < rows; ++i) {
free(mat[i]);
}
free(mat);
}
}
void array_to_matrix(int **matrix, const int *arr, int rows, int cols) {
for (int i = 0, k = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = arr[k++];
}
}
}
void print_matrix(int **matrix, int rows, int cols) {
printf("\n 2D matrix is as follows:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
我需要一些关于此函数的帮助,该函数根据输入中的数组指针进行排列。 这是代码。
#include <stdio.h>
#include <stdlib.h>
void input_array(int *arr, int size);
void print_array(int *arr, int size);
void array_to_matrix(int **matrix, int *arr, int row, int col);
void print_matrix(int **matrix, int row, int col);
int factorial(int n)
{
if (n == 0)
return 1;
else
return (n * factorial(n - 1));
}
void swap(int *x1, int *x2)
{
int x = *x1;
*x1 = *x2;
*x2 = x;
}
int *permute(int *new_arr, int st, int ls)
{
int i = 0;
if (st == ls) {
// int k;
// for (k = 0; k < ls; k++)
// {
// printf("%d ", new_arr[k]);
// }
// printf("\n");
return new_arr;
} else {
for (i = st; i < ls; i++)
{
swap(new_arr + st, new_arr + i);
permute(new_arr, st + 1, ls);
swap(new_arr + st, new_arr + i);
}
}
return new_arr;
}
int main()
{
int m, n, *arr, **mat;
int size, i;
//INIZIO CALCOLO MATRICE DI ARRAY INIZIALE (matrice dei costi)
//inserisci il numero di colonne, che corrisponde al numero di città
printf("Enter the number of columns(n):");
if ((scanf("%d", &n) != 1) || (n < 1)) {
puts("invalid value for columns");
return -1;
}
m = n;
//m=numero di righe n=numero di colonne
size = m * n;
if (((arr = malloc(size * sizeof(int))) == NULL) ||
((mat = malloc(m * sizeof(int))) == NULL)) {
puts("not enough memory");
exit(-1);
}
for (i = 0; i < m; ++i) {
if ((mat[i] = malloc(n * sizeof(int))) == NULL) {
puts("not enough memory");
exit(-1);
}
}
input_array(arr, size);
print_array(arr, size);
array_to_matrix(mat, arr, m, n);
print_matrix(mat, m, n);
for (i = 0; i < m; ++i)
free(mat[i]);
free(mat);
//INIZIO CALCOLO PERMUTAZIONI (matrice dei percorsi possibili)
int nrighe = factorial(n);
int *new_array;
int st = 0, k = 0, j;
if (((new_array = malloc((n * nrighe) * sizeof(int))) == NULL) ||
((mat = malloc((n * nrighe) * sizeof(int))) == NULL)) {
puts("not enough memory");
exit(-1);
}
for (i = 0; i < m; ++i) {
if ((mat[i] = malloc((n * nrighe) * sizeof(int))) == NULL) {
puts("not enough memory");
exit(-1);
}
}
for (j = 1; j < n + 1; j++) {
new_array[k] = j;
printf("newarray[%d", k);
printf("]= %d", j);
k++;
}
free(arr);
if ((arr = malloc((n * nrighe) * sizeof(int))) == NULL) {
puts("not enough memory");
exit(-1);
}
printf("\nPermutations of dimension: %d\n", n);
arr = permute(new_array, st, n);
k = 0;
for (k = 0; k < n; k++) {
printf("%d ", arr[k]);
}
printf("\n");
array_to_matrix(mat, new_array, nrighe, n);
print_matrix(mat, nrighe, n);
free(arr);
free(new_array);
return 0;
}
void input_array(int *arr, int size)
{
int i;
for (i = 0; i < size; i++) {
printf("Enter element a[%d]", i);
if (scanf("%d", &arr[i]) != 1) {
int c;
puts("invalid value, redo");
/* flush invalid value up to the end of line */
while ((c = getchar()) != '\n') {
if (c == EOF) {
puts("EOF, abort");
exit(-1);
}
}
i -= 1;
}
}
}
void print_array(int *arr, int size)
{
int i;
printf("\n 1D array is as follows : \n");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
void array_to_matrix(int **matrix, int *arr, int row, int col)
{
int i, j, k = 0;
for (i = 0; i < row; i++) {
for (j = 0;j < col; j++) {
matrix[i][j] = arr[k++];
}
}
}
void print_matrix(int **matrix, int row, int col)
{
int i, j;
printf("\n 2D matrix is as follows : \n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
当我像评论中的部分一样打印数组时,它起作用了。它打印它找到的所有排列。问题是当我尝试从 return 函数中打印它时,在 main()
函数中,我从 permute()
函数中获取数组 returned,然后使用 for
loop 我尝试打印它,但它说它是空的;循环不打印正确的值。我不明白为什么。有人可以帮我吗?
permute
会计算并且可以显示作为参数传递的数组的所有排列,但它不能 return 单独显示它们。如果您想将所有排列存储到一个矩阵中,您应该以这种方式将目标数组与索引一起传递:
int permute(int *arr, int st, int ls, int *new_array, int k)
{
int i;
if (st == ls) {
/* store a new permutation */
for (i = 0; i < ls; i++) {
new_array[k++] = arr[i];
}
} else {
for (i = st; i < ls; i++) {
swap(arr + st, arr + i);
k = permute(arr, st + 1, ls, new_array, k);
swap(arr + st, arr + i);
}
}
return k;
}
这是您的程序的修改版本,修复了更多错误并包括可以处理重复项的 permute
版本:
#include <stdio.h>
#include <stdlib.h>
void input_array(int *arr, int size);
void print_array(const int *arr, int size);
int **allocate_matrix(int rows, int cols);
void free_matrix(int **mat, int rows, int cols);
void array_to_matrix(int **matrix, const int *arr, int rows, int cols);
void print_matrix(int **matrix, int rows, int cols);
int factorial(int n) {
if (n <= 0)
return 1;
else
return n * factorial(n - 1);
}
void swap(int *x1, int *x2) {
int x = *x1;
*x1 = *x2;
*x2 = x;
}
int permute(int *arr, int st, int ls, int *dest, int k) {
if (st == ls) {
/* store a new permutation */
for (int i = 0; i < ls; i++) {
dest[k++] = arr[i];
}
} else {
k = permute(arr, st + 1, ls, dest, k);
for (int i = st + 1; i < ls; i++) {
/* eliminate permutations of identical elements */
if (arr[st] != arr[i]) {
swap(arr + st, arr + i);
k = permute(arr, st + 1, ls, dest, k);
swap(arr + st, arr + i);
}
}
}
return k;
}
int main() {
int n, nrighe, size, *arr, **mat;
//INIZIO CALCOLO MATRICE DI ARRAY INIZIALE (matrice dei costi)
//inserisci il numero di colonne, che corrisponde al numero di città
printf("Enter the number of columns(n): ");
if (scanf("%d", &n) != 1 || n < 1) {
fprintf(stderr, "invalid value for columns\n");
exit(1);
}
#if 1
nrighe = n;
//nrighe = number of rows, n = number of columns
size = nrighe * n;
if (((arr = calloc(size, sizeof(*arr))) == NULL)
|| ((mat = allocate_matrix(nrighe, n)) == NULL)) {
fprintf(stderr, "not enough memory\n");
exit(1);
}
input_array(arr, size);
print_array(arr, size);
array_to_matrix(mat, arr, nrighe, n);
print_matrix(mat, nrighe, n);
free_matrix(mat, nrighe, n);
#endif
//INIZIO CALCOLO PERMUTAZIONI (matrice dei percorsi possibili)
nrighe = factorial(n);
size = nrighe * n;
if (((arr = calloc(size, sizeof(*arr))) == NULL)
|| ((mat = allocate_matrix(nrighe, n)) == NULL)) {
fprintf(stderr, "not enough memory\n");
exit(1);
}
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
printf("newarray[%d] = %d\n", i, i + 1);
}
printf("\nPermutations of dimension: %d\n", n);
permute(arr, 0, n, arr, 0);
array_to_matrix(mat, arr, nrighe, n);
print_matrix(mat, nrighe, n);
free(arr);
free_matrix(mat, nrighe, n);
return 0;
}
void input_array(int *arr, int size) {
for (int i = 0; i < size; i++) {
printf("Enter element a[%d]: ", i);
if (scanf("%d", &arr[i]) != 1) {
int c;
fprintf(stderr, "invalid value, redo\n");
/* flush invalid value up to the end of line */
while ((c = getchar()) != '\n') {
if (c == EOF) {
fprintf(stderr, "EOF, abort\n");
exit(1);
}
}
i -= 1;
}
}
}
void print_array(const int *arr, int size) {
printf("\n 1D array is as follows:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
/* allocate a matrix of m rows and n columns initialized to 0 */
int **allocate_matrix(int rows, int cols) {
int **mat;
if ((mat = calloc(rows, sizeof(*mat))) == NULL)
return NULL;
for (int i = 0; i < rows; ++i) {
if ((mat[i] = calloc(cols, sizeof(*mat[i]))) == NULL) {
while (i-- > 0)
free(mat[i]);
free(mat);
return NULL;
}
}
return mat;
}
void free_matrix(int **mat, int rows, int cols) {
if (mat) {
for (int i = 0; i < rows; ++i) {
free(mat[i]);
}
free(mat);
}
}
void array_to_matrix(int **matrix, const int *arr, int rows, int cols) {
for (int i = 0, k = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = arr[k++];
}
}
}
void print_matrix(int **matrix, int rows, int cols) {
printf("\n 2D matrix is as follows:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}