如何使用递归打印出给定数组的每种可能的排列。

huangapple go评论52阅读模式
英文:

How to print out every possible arrangement of a given array using recursion

问题

你想要排列并打印给定数组的所有可能组合,但要排除重复项,只保留每个元素的一次使用。下面是修改后的代码以实现这一目标:

#include <stdio.h>
#include <stdlib.h>

typedef struct val_s val_t;
struct val_s
{
    float *choices;
    int used; // 添加一个标记来跟踪元素是否已经在当前排列中使用过
};

void multiplication_principle(val_t *val, float *sol, int n, int pos)
{
    if (pos == n)
    {
        // 检查是否有重复的元素,如果有则跳过
        int isDuplicate = 0;
        for (int i = 0; i < n - 1; ++i)
        {
            for (int j = i + 1; j < n; ++j)
            {
                if (sol[i] == sol[j])
                {
                    isDuplicate = 1;
                    break;
                }
            }
            if (isDuplicate)
                break;
        }

        if (!isDuplicate)
        {
            for (int i = 0; i < n; ++i)
            {
                printf("%3.0f ", sol[i]);
            }
            printf("\n");
        }
        return;
    }
    for (int i = 0; i < n; ++i)
    {
        if (!val[i].used) // 仅使用未被标记为已使用的元素
        {
            sol[pos] = val[i].choices[pos];
            val[i].used = 1; // 标记元素为已使用
            multiplication_principle(val, sol, n, pos + 1);
            val[i].used = 0; // 恢复元素状态,以便下一轮排列使用
        }
    }
}

int main()
{
    val_t *v;
    float *sol;
    float val[] = {+10, -5, +7, -8};
    int n = 4;
    v = malloc(n * sizeof(val_t));
    if (v == NULL)
    {
        exit(1);
    }
    for (int i = 0; i < n; ++i)
    {
        v[i].choices = malloc(n * sizeof(float));
        if (v[i].choices == NULL)
        {
            exit(1);
        }
        for (int j = 0; j < n; ++j)
        {
            v[i].choices[j] = val[j];
        }
        v[i].used = 0; // 初始化标记为未使用
    }
    sol = malloc(n * sizeof(float));
    if (sol == NULL)
    {
        exit(1);
    }
    multiplication_principle(v, sol, n, 0);

    return 0;
}

这个修改后的代码将排除重复的排列组合,只保留每个元素的一次使用,就像你所描述的那样。

英文:

We are given an array of elements float val[]={+10, -5, +7, -8}; and i want to print out every possible rearrangement of this matrix using recursion

I have kinda done this but in my textbook the only thing i found close to what i want is the multiplication principle

This is my code

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
typedef struct val_s val_t;
struct val_s
{
float *choices;
};
void multiplication_principle(val_t *val,float *sol,int n,int pos)
{
if(pos==n)
{
for (int i = 0; i &lt; n; ++i) {
printf(&quot;%3.0f &quot;,sol[i]);
}
printf(&quot;\n&quot;);
return;
}
for (int i = 0; i &lt; n; ++i) {
sol[pos]=val[pos].choices[i];
multiplication_principle(val,sol,n,pos+1);
}
}
int main() {
val_t *v;
float *sol;
float val[]={+10, -5, +7, -8};
int n=4;
v=malloc( n*sizeof(val_t));
if(val==NULL)
{
exit(1);
}
for (int i = 0; i &lt; n ; ++i) {
v[i].choices=malloc(n*sizeof (float ));
if(v[i].choices==NULL)
{
exit(1);
}
for (int j = 0; j &lt; n; ++j) {
v[i].choices[j]=val[j];
}
}
sol=malloc(n* sizeof(float));
if(sol==NULL)
{
exit(1);
}
multiplication_principle(v,sol,n,0);
return 0;
}

And these are the results(not all of them cuz i would have to paste here over 50 or so of them but i think you get the idea)

 10  10  10  -5
10  10  10   7
10  10  10  -8
10  10  -5  10
10  10  -5  -5
10  10  -5   7 
10  10  -5  -8 
10  10   7  10 
10  10   7  -5 
10  10   7   7 
10  10   7  -8 
10  10  -8  10 
10  10  -8  -5 
10  10  -8   7 
10  10  -8  -8 
10  -5  10  10 
10  -5  10  -5 
10  -5  10   7 
10  -5  10  -8 
10  -5  -5  10 
10  -5  -5  -5 
10  -5  -5   7 
10  -5  -5  -8 
10  -5   7  10 
10  -5   7  -5
10  -5   7   7 
10  -5   7  -8
10  -5  -8  10
10  -5  -8  -5
10  -5  -8   7
10  -5  -8  -8
10   7  10  10
10   7  10  -5
10   7  10   7
10   7  10  -8 
10  -8  10  -8
10  -8  -5  10
10  -8  -5  -5
10  -8  -5   7
10  -8  -5  -8
10  -8   7  10
10  -8   7  -5
10  -8   7   7
10  -8   7  -8
10  -8  -8  10
10   7  -8   7
10   7  -8  -8
10  -8  10  10
10  -8  10  -5
10  -8  10   7
10  -8  -8  -5
10  -8  -8   7
10  -8  -8  -8
-5  10  10  10
-5  10  10  -5 
-5  10  10   7 
-5  10  10  -8
-5  10  -5  10
-5  10  -5  -5
-5  10  -5   7
-5  10  -5  -8
-5  10   7  10
-5  10   7  -5
-5  10   7   7
-5  10   7  -8
-5  10  -8  10
-5  10  -8  -5 
-5  10  -8   7
-5  10  -8  -8
-5  -5  10  10
-5  -5  10  -5
-5  -5  10   7
-5  -5  10  -8
-5  -5  -5  10
-5  -5  -5  -5
-5  -5  -5   7
-5  -5  -5  -8
-5  -5   7  10 
-5  -5   7  -5
-5  -5   7   7
-5  -5   7  -8
-5  -5  -8  10

What i want though is to exclude the repetitions. where i only have the elements used one time like for example

10  -5   7  -8
-5  -10   7  -8
7  -5   10  -8

and so on

答案1

得分: 3

"Never mind i managed to do it (GREIT SUCCESS)"

#include <stdio.h>
#include <stdlib.h>

void permutation(float *val, float *sol, float *mark, int n, int pos)
{
    if (pos == n)
    {
        for (int i = 0; i < n; ++i)
        {
            printf("%.0f ", sol[i]);
        }
        printf("\n");
        return;
    }
    for (int i = 0; i < n; ++i)
    {
        if (mark[i] == 0)
        {
            mark[i] = 1;
            sol[pos] = val[i];
            permutation(val, sol, mark, n, pos + 1);
            mark[i] = 0;
        }
    }
}
int main()
{
    float *sol;
    float *val;
    int n = 4;
    float *mark;
    mark = calloc(n, sizeof(float));
    if (mark == NULL)
    {
        exit(1);
    }
    val = malloc(n * sizeof(float));
    if (val == NULL)
    {
        exit(1);
    }
    val = (float[]){+10, -5, +7, -8};

    sol = malloc(n * sizeof(float));
    if (sol == NULL)
    {
        exit(1);
    }

    permutation(val, sol, mark, n, 0);
    return 0;
}

And the results

10 -5 7 -8
10 -5 -8 7
10 7 -5 -8
10 7 -8 -5
10 -8 -5 7
10 -8 7 -5
-5 10 7 -8
-5 10 -8 7
-5 7 10 -8
-5 7 -8 10
-5 -8 10 7
-5 -8 7 10
7 10 -5 -8
7 10 -8 -5
7 -5 10 -8
7 -5 -8 10
7 -8 10 -5
7 -8 -5 10
-8 10 -5 7
-8 10 7 -5
-8 -5 10 7
-8 -5 7 10
-8 7 10 -5
-8 7 -5 10
英文:

Never mind i managed to do it (GREIT SUCCESS)

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
void permutation(float *val,float *sol,float *mark,int n,int pos)
{
if(pos==n)
{
for (int i = 0; i &lt; n; ++i) {
printf(&quot;%.0f &quot;,sol[i]);
}
printf(&quot;\n&quot;);
return;
}
for (int i = 0; i &lt; n; ++i) {
if(mark[i]==0)
{
mark[i]=1;
sol[pos]=val[i];
permutation(val,sol,mark,n,pos+1);
mark[i]=0;
}
}
}
int main() {
float *sol;
float *val;
int n=4;
float *mark;
mark = calloc(n, sizeof(float ));
if(mark==NULL)
{
exit(1);
}
val = malloc(n* sizeof(float ));
if(val==NULL)
{
exit(1);
}
val=(float []){+10, -5, +7, -8};
sol=malloc(n* sizeof(float));
if(sol==NULL)
{
exit(1);
}
permutation(val,sol,mark,n,0);
return 0;
}

And the results

10 -5 7 -8
10 -5 -8 7
10 7 -5 -8
10 7 -8 -5
10 -8 -5 7
10 -8 7 -5
-5 10 7 -8
-5 10 -8 7
-5 7 10 -8
-5 7 -8 10
-5 -8 10 7
-5 -8 7 10
7 10 -5 -8
7 10 -8 -5
7 -5 10 -8
7 -5 -8 10
7 -8 10 -5
7 -8 -5 10
-8 10 -5 7
-8 10 7 -5
-8 -5 10 7
-8 -5 7 10
-8 7 10 -5
-8 7 -5 10

huangapple
  • 本文由 发表于 2023年5月26日 10:19:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定