gpt4 book ai didi

c - 如何按照规则对数组进行合并和排序

转载 作者:行者123 更新时间:2023-11-30 16:51:04 24 4
gpt4 key购买 nike

我有一个问题。我需要执行一个接收 2 个大小为 n、m 的数组的函数。并返回一个新数组,其中包含最小长度的常见数字。 示例:

      A: 2 5 3 1 2 4 6 2 4 3 5 2

B: 7 5 2 5 8 1 2 5 9 2

新数组看起来像:

      C:  1 2 2 2 5 5 

新数组的大小必须为 6。

这是我到目前为止所做的事情。 (但它不起作用,我找不到问题)

 int *same_same(int *ar1, int *ar2,int n, int m)
{
int *c;
int i=0,j=0,k=0;
int min=n;
if (n>m)
min=m;
c=(int *)calloc(min, sizeof(int));
bubble_sort (ar1, n);
bubble_sort (ar2, m);

while( i<n && j<m )
{
if (ar1[i] == ar2[j])
{
c[k++]=ar1[i++];
j++;

}
else if (ar1[i] < ar2[j])
i++;
else j++;

}
c=(int *)realloc(k, sizeof(int));
return c;
free (c);
}

完整代码为:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <assert.h>
int *input_array_dyn(int n)
{
int i;
int *a;
a=(int *)calloc(n, sizeof(int));
assert(a);
printf("enter the array of length %d\n",n);
for(i=0;i<n;i++)
scanf_s("%lf",a+i);
return a;
}
void swap(int *v,int *u) //function aid for ex3
{
int temp;
temp=*v;
*v=*u;
*u=temp;
}
void bubble_sort(int *a, int n)
{
int i,j;
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
swap(&a[j],&a[j+1]);

}
int *same_same(int *ar1, int *ar2,int n, int m)
{
int *c;
int i=0,j=0,k=0;
int min=n;
if (n>m)
min=m;
c=(int *)calloc(min, sizeof(int));
bubble_sort (ar1, n);
bubble_sort (ar2, m);

while( i<n && j<m )
{
if (ar1[i] == ar2[j])
{
c[k++]=ar1[i++];
j++;

}
else if (ar1[i] < ar2[j])
i++;
else j++;

}
c=(int *)realloc(k, sizeof(int));
return c;
free (c);
}
int main(void)
{
int *arr1 , *arr2 ,n,m; // input for Ex 3
printf_s("Please Insert Size of array one :\n");
scanf_s("%d",&n);
printf_s("Please Insert Size of array two :\n");
scanf_s("%d",&m);
printf_s("Please Insert numbers of array one :\n");
arr1=input_array_dyn(n);
printf_s("Please Insert numbers of array two :\n");
arr2=input_array_dyn(m);
printf_s("The new array is :\n");
same_same(arr1,arr2,n,m);
free (arr1);
free (arr2);

}

最佳答案

这是已修复的版本。调用 realloc() 并不能保证返回的指针位于同一位置。根据http://www.cplusplus.com/reference/cstdlib/realloc/“返回值。指向重新分配的内存块的指针,可以与 ptr 相同,也可以是新位置。”

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <string.h>

void swap(int *v,int *u) //function aid for ex3
{
int temp;
temp=*v;
*v=*u;
*u=temp;
}

void bubble_sort(int *a, int n)
{
int i,j;
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
swap(&a[j],&a[j+1]);

}

void *same_same(int *ar1, int *ar2,int n, int m, int ** res, int * res_size)
{
int *c;
int i=0,j=0,k=0;
int min=n;
if (n>m)
min=m;
c=(int *)calloc(min, sizeof(int));
bubble_sort (ar1, n);
bubble_sort (ar2, m);

while( i<n && j<m )
{
if (ar1[i] == ar2[j])
{
c[k++]=ar1[i++];
j++;

}
else if (ar1[i] < ar2[j]){
i++;}
else j++;

}

// Fixes
*res_size = k;
(*res) = (int *) calloc(*res_size, sizeof(int));
memcpy(*res, c, (*res_size)*sizeof(int));
free(c);
}

//Example code
int main(void)
{
int arr1[] = {2, 5, 3, 1, 2, 4, 6, 2, 4, 3, 5, 2};
int arr2[] = {7, 5, 2, 5, 8, 1, 2, 5, 9, 2};
int n = sizeof(arr1)/sizeof(int);
int m = sizeof(arr2)/sizeof(int);
// input for Ex 3
int *res;
int res_size;
same_same(arr1,arr2,n,m, &res, &res_size);
int i;
for(i=0; i < res_size; i++)
printf("%d ", res[i]);
printf("\n");

return 0;
}

关于c - 如何按照规则对数组进行合并和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41909071/

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