gpt4 book ai didi

c - C 中的函数,实现两个 int 数组在另一个数组中的数学并集

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:34:19 25 4
gpt4 key购买 nike

<分区>

语言是 ANSI C。我有 2 个 int 数组:ABA 有一个名为 m 的索引,B 有一个名为 n 的索引。赋值说明 m 必须不同于 n,因此数组的大小必须不同。我已经编码了。 A 按升序排列,而 B 按降序排列。我必须编写一个函数,在另一个名为 C 的数组中执行两个数组的数学并集。如果一个元素在两个数组中,您只需将一个元素放入 union 数组(数组 C)。

我的代码运行得不是很好。最后一个元素未排序,我收到一个输出,其中有一个非常大的最后一个数字,我不知道它来自哪里。

int index_c=index_m+index_n; //the index of array c
// is obtained by the sum of two indexes of the array A and B
int c[index_c];
int k=0;
for (i=0; i < index_m; i++)
{
for (j=0; j < index_n; j++)
{
if (a[i]==b[j])
{
c[k]=a[i]; //put only one time if is repeated more time in the two arrays
}
else
{
c[k]=a[i]; //put the a[i] element in the array c
c[k+1]=b[j]; //the element of the other array next to
}

}
k++;
}

printf("Elements in array C are: \n");
for (i=0; i<index_c; i++)
printf("element %d\n", c[i]);

数组C没排序也没关系,我会在union之后排序。有什么建议吗?

我正在尝试在添加 1 个输入时使用 put k++,在将两个输入添加到数组 C 时使用 k+2。现在它工作得有点好,但还不能完全工作。我的意思是在输出中我没有大数值,但其中一个输出值(第三个)与第一个相同。示例:3 9 3 2 5 第二个 3 是错误的,它缺少一个被第二个 3 覆盖的数字。其他例子 2 4 2 1 9

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com