使用C,在N个数组中找到唯一的数组 查找n个数组中的唯一数组,考虑我有3个数组;
使用C,在N个数组中找到唯一的数组 查找n个数组中的唯一数组,考虑我有3个数组;,c,arrays,C,Arrays,array_1 [ ]; array_2 [ ]; array_3 [ ]; How I approached is; I will have set a reference array ref_array[] =[values from 1 to 12] 可以保证所有阵列都有1到12个随机元素 e.g 1;array_1[7,8,9] , array_2[1,2,3,4] , array_3[ 2,4,10,11].. during the pr
array_1 [ ];
array_2 [ ];
array_3 [ ];
How I approached is;
I will have set a reference array
ref_array[] =[values from 1 to 12]
可以保证所有阵列都有1到12个随机元素
e.g 1;array_1[7,8,9] ,
array_2[1,2,3,4] ,
array_3[ 2,4,10,11]..
during the process of array_1 loop (7,8,9 are marked to zero in reference array)
and in next loop of array_2 (1,2,3,4 are marked to zero in reference array) ,
and in next loop of array_3( 2,4 wont have matching element so store the array num which is three from this example), so array_1 and array_2 are unique among the three arrays
e、 g 2详细说明
+如果数组_1和ref_数组之间存在匹配元素,请将相应的ref_数组元素标记为零
e.g array_1 has three elements[ 7,10,9]
*array_1 7 is = ref_array 7 then
mark ref_array[k]=0;
so that there is no 7 in ref_array
*array_1 10 is = ref_array 10 then
mark ref_array[k]=0;
so that there is no 10 in ref_array
*array_1 9 is = ref_array 9
mark ref_array[k]=0;
so that there is no 9 in ref_array
+ now next array comes into the loop
e.g array_2 has two elements[ 10, 1]
*array_2 10is != ref_array [k]
now element 10 is not found in the reference array
then store the value of 'i'
here i is the route num(which is array_num, in this example it is 2)
array_2 2is = ref_array 2
mark ref_array[k]=0;
so that there is no 2 in ref_array.
+ e.g array_3 has three elements[ 2,7,11,10]
*same procedure follows, element 2,7,10 has no matching to reference array.
so the value of i is stored (which is array_num, in this example it is 3)
+ solution:
so the array_1 is unique among the three arrays
需要建议和指导
我试过使用我的代码,但在解决方案部分遇到了问题
#include <stdio.h>
struct route{
int route[6][6];
int no_routes_found;
int count_each_route[6];
};
int main() {
struct route *route_ptr, route_store;
route_ptr=&route_store;
int ref_array[13];
int count_ref_array=13;
int i,j,k;
int found_indices[18];
int index_count=0;
int store;
int count=1;
int random;
int flag=0;
// intializing the reference array
for (i = 0; i < 13; i++)
{
ref_array[i]= count++;
printf("\nref_array[%d] = %d", i,ref_array[i]);
}
// initializing the structure
route_ptr->no_routes_found=3;
route_ptr-> count_each_route[0]=3;
route_ptr-> count_each_route[1]=2;
route_ptr-> count_each_route[2]=4;
for (i = 0; i < 6; i++)
{
for (j = 0; j < 6; j++)
{
route_ptr->route[i][j] = rand() % 12 +1;
printf("\nroute[%d][%d] = %d", i,j,route_ptr->route[i][j]);
}
}
//// solution part
for (i = 0; i <(route_ptr->no_routes_found) ; i++)
{
for (j = 0; j < route_ptr-> count_each_route[i]; j++)
{
for(k=0;k<(count_ref_array) ;k++)
{
if (route_ptr->route[i][j]==ref_array[k])
{
printf("\nroute %dis = ref_array %d",route_ptr->route[i][j], ref_array[k]);
// here is where i need the solution
}
}
}
}
}
#包括
结构路由{
国际航线[6][6];
int未找到任何路由;
每条路线的整数计数[6];
};
int main(){
结构路由*路由ptr,路由存储;
route_ptr=&route_store;
int-ref_数组[13];
int count_ref_array=13;
int i,j,k;
int-found_指数[18];
int index_count=0;
国际商店;
整数计数=1;
int随机;
int标志=0;
//初始化引用数组
对于(i=0;i<13;i++)
{
ref_数组[i]=count++;
printf(“\nref_数组[%d]=%d”,i,ref_数组[i]);
}
//初始化结构
路由\u ptr->未找到路由\u=3;
路由\u ptr->count\u each\u route[0]=3;
路由\u ptr->count\u each\u route[1]=2;
路由ptr->count\u每条路由[2]=4;
对于(i=0;i<6;i++)
{
对于(j=0;j<6;j++)
{
路由\u ptr->route[i][j]=rand()%12+1;
printf(“\n路由[%d][%d]=%d”,i,j,路由\u ptr->路由[i][j]);
}
}
////解决方案部分
对于(i=0;i未找到任何路由);i++)
{
对于(j=0;jcount_each_route[i];j++)
{
对于(k=0;kroute[i][j]==ref_数组[k])
{
printf(“\n路由%dis=ref_数组%d”,路由ptr->route[i][j],ref_数组[k]);
//这就是我需要解决的地方
}
}
}
}
}
如何找到与其他阵列不同的阵列?通过将每个数组与第一个数组进行比较。因为如果前两个不同,你无法分辨哪个是单数,所以需要特殊的案例代码
int equal(int*a0, int *a1, size_t array_elems) {
return !memcmp(a0, a1, array_elems * sizeof(int));
}
int unequal_array(int **arrays, int N, size_t array_elems) {
if (!equal(arrays[0], arrays[1], array_elems))
return equal(arrays[0], arrays[2], array_elems) ? 1 : 0;
for (int i = 2; i < N; i++) {
if (!equal(arrays[0], arrays[i], array_elems)) return i;
}
return -1;
}
int相等(int*a0,int*a1,大小数组元素){
return!memcmp(a0,a1,数组元素*sizeof(int));
}
整数不等数组(整数**数组,整数N,大小数组元素){
if(!equal(数组[0],数组[1],数组元素))
返回相等(数组[0],数组[2],数组元素)?1:0;
for(int i=2;i
那么实际的问题是什么,或者在,您能提供一些关于代码中什么不起作用的信息吗?从我的解决方案部分,我只能找出数组中有匹配元素和引用数组,如果有匹配元素,我无法继续,将“0”标记到相应的引用数组,如果下一个元素没有与引用数组匹配的元素,则存储数组编号的值。如果,数组_1=[7,10,9];数组_2=[10,1];数组_3=[2,7,11,10]。结果应该只提供数组_1是uniquee.g数组_1[7,8,9],数组_2[1,2,3,4],数组_3[2,4,10,11]。。在数组_1循环(参考数组中的7,8,9标记为零)和数组_2的下一个循环(参考数组中的1,2,3,4标记为零)以及数组_3的下一个循环(2,4不会有匹配的元素,因此存储本例中的数组编号3)的过程中,因此,数组_1和数组_2在这三个数组中是唯一的。您在数组_3中的描述有三个元素[2,7,11,10]
令人困惑;它看起来很像数组中的4个元素,尽管后面的描述没有提到11。是的,如果前两个数组不同,那么第三个数组将有不同的问题。这就是我使用参考数组的原因,如果元素匹配,则将其标记为零,如果元素不匹配,则存储相应的数组编号。g array_1[7,8,9]、array_2[1,2,3,4]、array_3[2,4,10,11]。。在数组_1循环过程中(参考数组中7,8,9标记为零),在数组_2的下一个循环过程中(参考数组中1,2,3,4标记为零),在数组_3的下一个循环过程中(2,4不会有匹配的元素,因此存储本例中为3的数组编号)。