我有2个数组。输入1和输入2.假设输入1具有{1,1,2,3,3,4,5},输入2具有{4,2,6,7,8}。合并数组{1,1,2,3,3,4,5,4,2,6,7,8}
排序后,我的合并数组看起来像{1,1,2,2,3,3,4,4,5,6,7,8}
我的输出应该像{5,6,7,8},因为它们是非重复元素。它应该既没有在第一次发生两次,也没有在第一次发生一次,在第二次发生一
我已将两个数组放入一个单独的数组中(两者合并)。对它进行排序并删除重复项。我得到的输出像{1,2,3,4,5,6,7,8},但我不应该得到1,2,3,4,因为它们在合并数组中都发生了两次。
请帮我尽快完成该计划。提前谢谢。
我不能使用结构。那是我的任务。
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],b[10],c[10],i,j,n,n1,temp;
clrscr();
printf("enter the no of ele in array1\n");
scanf("%d",&n);
printf("enter 1st array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter size of 2nd array");
scanf("%d",&n1);
printf("enter 2nd array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
c[i]=a[i];
}
j=0;
for(i=n;i<n+n1;i++)
{
c[i]=b[j];
// printf("\n2nd array values are %d",c[i]);
j++;
}
for(i=0;i<n+n1;i++)
{
printf("%d\n",c[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(c[j]>c[j+1])
{
temp=c[j];
c[j]=c[j+1];
c[j+1]=temp;
}
}
}
for(j=0;j<n+n1;j++)
{
printf("sorted is %d\n",c[j]);
}
for(i=0;i<n+n1;i++)
{
if(c[i]==c[i+1])
{
temp=c[i];
// printf("%d r d rep.ele\n",c[i]);
}
else
printf("%d r d ele\n",c[i]);
}
getch();
}
答案 0 :(得分:1)
让我们以不恰当但又有趣的方式解决这个问题:
#include <stdio.h>
#define MAXINPUT 10
void pack(const int* k, int n, int* table)
{
for(int i = 0; i < n; ++i)
{
table[k[i]]++;
}
}
void dump(const int* table)
{
int flag = 0;
for(int i = 0; i < MAXINPUT; ++i)
{
if(table[i] == 1)
{
printf("%s%d", (flag) ? ", " : "", i);
flag = 1;
}
}
}
int c[MAXINPUT];
int main(void)
{
const int a[] = { 1, 1, 2, 3, 3, 4, 5};
const int b[] = { 4, 2, 6, 7, 8};
pack(&a[0], sizeof(a)/sizeof(int), &c[0]);
pack(&b[0], sizeof(b)/sizeof(int), &c[0]);
dump(&c[0]);
return 0;
}
答案 1 :(得分:0)
size_t i, non_repeating_index = 0;
// allocate memory to hold the new array
int * non_repeating_elements = calloc(sizeof(int), merged_array_length);
// check if the first element is unique
if (merged_array_length >= 2 && merged_array[0] != merged_array[1])
non_repeating_elements[non_repeating_index++] = merged_array[0];
// for all other elemnts check if it equals the ones around it
for (i = 1; i < merged_array_length -1; i++) {
if (merged_array[i-1] != merged_array[i] && merged_array[i] != merged_array[i + 1])
non_repeating_elements[non_repeating_index++] = merged_array[i];
}
// check the last element
if (merged_array_length >= 3 && merged_array[i-1] != merged_array[i])
non_repeating_elements[non_repeating_index++] = merged_array[i];
// resize the array to conserve memory
non_repeating_elements = realloc(non_repeating_elements, sizeof(*non_repeating_elements) * non_repeating_index);
答案 2 :(得分:0)
你快到了!不要试图花哨,只需创建一个包含两个int字段的结构:value和count。创建这些结构的数组并遍历排序列表。最后,遍历结构数组并选择相应计数为1的所有值。
有一些细节可以解决,比如你的结构数组应该有多大,但是嘿,你显然不怕编码。如果阵列比你需要的大,那么......只要它足够大就关心它?
答案 3 :(得分:0)
如果你已经对数组进行了排序和合并,那么它非常简单:只需与前一代产品进行比较即可。接班人。但是需要一些额外的逻辑才能超出范围。
#include <stdio.h>
int main(int argc, char** argv){
const int sortedMergedArray[] = {1,1,2,2,3,3,4,4,5,6,7,8};
const size_t mergedArrayLength = 12;
for(size_t i=0; i<mergedArrayLength;i++){
int predecessor = -1;
int successor = -1;
if(i >= 1) predecessor = sortedMergedArray[i-1];
if(i <= mergedArrayLength-1) successor = sortedMergedArray[i+1];
if(sortedMergedArray[i] != successor &&
sortedMergedArray[i] != predecessor){
printf("%d, ", sortedMergedArray[i]);
}
}
return 0;
}
输出:
5, 6, 7, 8,
答案 4 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define NUMCMP(x,y) (((x) < (y)) ? -1 : ((x) > (y)) ? 1 : 0)
#define numcmp(type) int cmp_##type(const void *a, const void *b){\
type x = *(type*)a;\
type y = *(type*)b;\
return NUMCMP(x,y);\
}
numcmp(int);//define function cmp_int
void print_array(int a[], size_t size){
size_t i;
printf("{");
for(i=0;i<size;++i){
printf("%d", a[i]);
if(i != size-1)
printf(",");
}
printf("}\n");
}
int main(void){
int input1[] = {1,1,2,3,3,4,5};
int input2[] = {4,2,6,7,8};
size_t input1_size = sizeof(input1)/sizeof(*input1);
size_t input2_size = sizeof(input2)/sizeof(*input2);
printf("input1: ");
print_array(input1, input1_size);
printf("input2: ");
print_array(input2, input2_size);
size_t marge_array_size = input1_size + input2_size;
int marge_array[marge_array_size];
memcpy(marge_array, input1, input1_size*sizeof(*input1));
memcpy(&marge_array[input1_size], input2, input2_size*sizeof(*input2));
printf("marge array: ");
print_array(marge_array, marge_array_size);
qsort(marge_array, marge_array_size, sizeof(*marge_array), cmp_int);
printf("marge array(after sort): ");
print_array(marge_array, marge_array_size);
size_t singles_size = marge_array_size;
int *singles = malloc(singles_size * sizeof(*singles));
size_t i;
int sp;
bool drop = false;
for(sp=-1, i=0;i<marge_array_size;++i){
if(!drop){
if(sp==-1){
singles[++sp] = marge_array[i];
continue;
}
if(singles[sp] == marge_array[i]){
--sp;
drop = true;
continue;
}
} else if(singles[sp+1] == marge_array[i])
continue;
drop = false;
singles[++sp] = marge_array[i];
}
if(sp+1 != singles_size){
singles_size = sp+1;
singles = realloc(singles, singles_size*sizeof(*singles));
}
printf("marge array(single num): ");
print_array(singles, singles_size);
size_t uniques_size = marge_array_size;
int *uniques = malloc(uniques_size * sizeof(*uniques));
for(sp=-1, i=0;i<marge_array_size;++i){
if(sp==-1){
uniques[++sp] = marge_array[i];
continue;
}
if(uniques[sp] == marge_array[i]){
continue;
}
uniques[++sp] = marge_array[i];
}
if(sp+1 != uniques_size){
uniques_size = sp+1;
uniques = realloc(uniques, uniques_size*sizeof(*uniques));
}
printf("marge array(uniques num): ");
print_array(uniques, uniques_size);
//be free uniques and singles
return 0;
}
答案 5 :(得分:0)
#include<stdio.h>
int main()
{
int a[30],b[30];
int n,k,i,j,p,c=0;
printf("How many element in first array\n");
scanf("%d",&n);
printf("Enter elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]); //FIRST ARRAY INPUT
}
printf("How many element in second array\n");
scanf("%d",&k);
printf("Enter elements");
for(i=0;i<k;i++)
{
scanf("%d",&b[i]); // SECOND ARRAY INPUT
}
printf("The new array is \n");
for(i=n,j=0;i<(n+k);i++)
{
a[i]=b[j++]; // MERGE ARRAY B[30],IN A[30] BY EXTEND IT.
}
for(i=0;i<(n+k);i++)
{
printf("%d ",a[i]); // PRINT THE MERGED ARRAY
}
for(i=0,p=0;i<(n+k);i++) // ELIMINATE DUPLICATE ELEMENTS
{
if(a[i]!=NULL)
{
for(j=i+1;j<(n+k);j++)
{
if(a[i]==a[j]) // IF ANY ELEMENTS MATCH TO NEXT OTHERS ARRAY ELEMENTS THEN, THOSE ELEMENTS ARE ASSIGNED BY NULL.
{
a[j]=NULL;
}
}
a[p++]=a[i];
c++; // COUNTING FOR SIZE OF NEW ARRAY
}
}
printf("\nAfter eliminate the duplicate elements,the new array is : \n");
for(i=0;i<c;i++)
{
printf("%d ",a[i]); // PRINT NEW ARRAY
}
return 0;
}