删除 C 数组中的重复项
Removing duplicates in a C array
我正在编写一个程序来确定 2 个整数数组(10 个元素的大小)的交集。我想我已经涵盖了所有其他部分,除了整理重复项。有谁知道无需创建函数或使用外部 C 库即可检查重复项的方法吗?
#include <stdio.h>
#define SIZE 10
int main(void){
//Initialization
int array1[SIZE];
for (int i = 0; i < SIZE; i++)
{
printf("Input integer %d of set A: ", i + 1);
scanf("%d", &array1[i]);
}
int array2[SIZE];
for (int i = 0; i < SIZE; i++)
{
printf("Input integer %d of set B: ", i + 1);
scanf("%d", &array2[i]);
}
int intersection[SIZE];
for (int i = 0; i < SIZE; i++)
{
intersection[i] = '[=10=]';
}
//Intersection check
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (array1[i] == array2[j])
{
intersection[i] = array1[i];
break;
}
}
}
//duplicate check
int count = SIZE;
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
if (intersection[i] == intersection[j])
{
for (int k = j; j < count; i++)
{
intersection[k] = intersection[k + 1];
}
count--;
}
}
}
//printing set
for (int i = 0; i < SIZE ; i++)
{
//printf("%d\n", intersection[i]);
if (intersection[i] != '[=10=]')
{
printf("%d\n", intersection[i]);
}
}
return 0;
}
在上面的代码中,我尝试了一种方法,但它不起作用,而是在输入所有元素后使程序卡住。我对其他方法持开放态度,只要它不需要外部库即可 运行。谢谢
正如我现在所见,在检查重复项的第三个循环中,我必须增加 k 而不是我:
对于 (int k = j; j < count; k++),你还必须在你的代码中减少 j 的大小 count--;。所以你检查重复项的代码似乎是正确的但是,你想要这两个数组的交集你做了 ,所以你不必检查重复项,因为在数组交集 [SIZE] 中,你只会从两个数组中放置一个数字,所以你不会有重复项。如果你想合并,你应该检查重复项这两个数组。我根据您想创建的内容对您的代码进行了一些更改,此代码在此处找到两个 arrays.Try 的交集并删除重复检查,因为这会使您的代码变为 运行 无穷大。最后一件事,您的交叉路口检查必须替换为:
//路口检查
int i = 0, j = 0,k=0; // k is for the intersection array !
while (i < SIZE && j < SIZE) {
if (array1[i] < array2[j])
i++;
else if (array2[j] < array1[i])
j++;
else if(array1[i]==array2[j]) // if array1[i] == array2[j]
{
intersection[k]=array2[j];
//printf("intersection[%d]=%d\n",i,intersection[i]);
intersectCount++;
k++;
i++;
j++;
}
}
printf("intersectCount=%d\n",intersectCount);
我正在编写一个程序来确定 2 个整数数组(10 个元素的大小)的交集。我想我已经涵盖了所有其他部分,除了整理重复项。有谁知道无需创建函数或使用外部 C 库即可检查重复项的方法吗?
#include <stdio.h>
#define SIZE 10
int main(void){
//Initialization
int array1[SIZE];
for (int i = 0; i < SIZE; i++)
{
printf("Input integer %d of set A: ", i + 1);
scanf("%d", &array1[i]);
}
int array2[SIZE];
for (int i = 0; i < SIZE; i++)
{
printf("Input integer %d of set B: ", i + 1);
scanf("%d", &array2[i]);
}
int intersection[SIZE];
for (int i = 0; i < SIZE; i++)
{
intersection[i] = '[=10=]';
}
//Intersection check
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (array1[i] == array2[j])
{
intersection[i] = array1[i];
break;
}
}
}
//duplicate check
int count = SIZE;
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
if (intersection[i] == intersection[j])
{
for (int k = j; j < count; i++)
{
intersection[k] = intersection[k + 1];
}
count--;
}
}
}
//printing set
for (int i = 0; i < SIZE ; i++)
{
//printf("%d\n", intersection[i]);
if (intersection[i] != '[=10=]')
{
printf("%d\n", intersection[i]);
}
}
return 0;
}
在上面的代码中,我尝试了一种方法,但它不起作用,而是在输入所有元素后使程序卡住。我对其他方法持开放态度,只要它不需要外部库即可 运行。谢谢
正如我现在所见,在检查重复项的第三个循环中,我必须增加 k 而不是我: 对于 (int k = j; j < count; k++),你还必须在你的代码中减少 j 的大小 count--;。所以你检查重复项的代码似乎是正确的但是,你想要这两个数组的交集你做了 ,所以你不必检查重复项,因为在数组交集 [SIZE] 中,你只会从两个数组中放置一个数字,所以你不会有重复项。如果你想合并,你应该检查重复项这两个数组。我根据您想创建的内容对您的代码进行了一些更改,此代码在此处找到两个 arrays.Try 的交集并删除重复检查,因为这会使您的代码变为 运行 无穷大。最后一件事,您的交叉路口检查必须替换为:
//路口检查
int i = 0, j = 0,k=0; // k is for the intersection array !
while (i < SIZE && j < SIZE) {
if (array1[i] < array2[j])
i++;
else if (array2[j] < array1[i])
j++;
else if(array1[i]==array2[j]) // if array1[i] == array2[j]
{
intersection[k]=array2[j];
//printf("intersection[%d]=%d\n",i,intersection[i]);
intersectCount++;
k++;
i++;
j++;
}
}
printf("intersectCount=%d\n",intersectCount);