2013-02-10 76 views
0

要在N个阵列中查找唯一阵列,请考虑我有3个阵列;使用C,在N个阵列中查找唯一阵列

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 

例如2 +如果有array_1和ref_array之间的匹配元件,标记随机元素相应的ref_array元素为零。

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 
       } 

      } 
     } 
    } 

} 
+2

什么是真正的问题,或者,你能提供一些信息,说明什么在代码中不起作用? – 2013-02-10 15:58:40

+0

从我的解决方案部分,我只能弄清楚数组有匹配元素与参考数组,我不能继续,如果有匹配的元素,标记'0'到相应的参考数组,如果下一个元素没有匹配元素到引用数组然后存储数组的数值。 – 2013-02-10 16:02:08

+0

if,array_1 = [7,10,9]; array_2 = [10,1]; array_3 = [2,7,11,10]。结果应该只提供array_1是唯一的 – 2013-02-10 16:09:33

回答

1

如何找到这与所有其他不同的阵列有问题吗?通过比较每个到第一个数组。因为如果前两个不同,你不能分辨出哪一个是奇数,所以需要特殊的代码。

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; 
} 
+0

是的,如果前两个数组不同,那么第三个数组会有不同的问题。多数民众赞成的原因,我把参考数组和标记为零,如果元素匹配,如果元素不匹配,然后存储相应的array_number – 2013-02-10 16:15:44

+0

例如array_1 [7,8,9],array_2 [1,2,3,4],array_3 [ 2,4,10,11] ..在array_1循环过程中(7,8,9在参考数组中标记为零)和array_2的下一个循环(1,2,3,4在参考中标记为零数组),并在array_3的下一个循环中(2,4不会有匹配的元素,所以存储数组num,这是本例中的三个数组)。 – 2013-02-10 16:19:01