0

我需要在二维数组中找到重复的元素。

route_ptr->route[0][1] = 24;
route_ptr->route[0][2] = 18;
route_ptr->route[1][1] = 25;
route_ptr->route[2][1] = 18;
route_ptr->route[3][1] = 26;
route_ptr->route[3][2] = 19;
route_ptr->route[4][1] = 25;
route_ptr->route[4][2] = 84;

这些是我的数据;必须找到 route[2][1](route[0][2] 的副本)和 route[4][1](route[1][1] 的副本)的重复条目。

解决方案是 route[i][j] 的重复 'i' 值,即本例中的 2 和 4。

请指导我。

#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 i,j,k;

    // the data
    route_ptr->route[0][1] = 24;
    route_ptr->route[0][2] = 18;
    route_ptr->route[1][1] = 25;
    route_ptr->route[2][1] = 18;
    route_ptr->route[3][1] = 26;
    route_ptr->route[3][2] = 19;
    route_ptr->route[4][1] = 25;
    route_ptr->route[4][2] = 84;
    route_ptr->count_each_route[0]=3;
    route_ptr->count_each_route[1]=2;
    route_ptr->count_each_route[2]=2;
    route_ptr->count_each_route[3]=3;
    route_ptr->count_each_route[4]=3;
    route_ptr->no_routes_found=5;

    ////  process
    for (i = 0; i <(route_ptr->no_routes_found) ; i++)
    {
        for (j = 1; j < route_ptr->count_each_route[i]; j++)
        {
            printf("\nroute[%d][%d] = ", i, j);
            printf("%d",route_ptr->route[i][j]);
        }
    }
}

预期的解决方案是:

route[0][1] is compared by route [0][2] i.e [24 !=18]
route[0][1] and route [0][2] is compared by route[1][1] i.e [24 && 18 !=25]
route[0][1] and route[0][2] and route[1][1] is compared by route[2][1] i.e [ 24&&18&&25 is compared by 18, there is a matching element,
   save the newcomer 'i' value which matches to the existence and drop it for next checking]
   break the 'i' loop
route[0][1], route[0][2], route[1][1] is now compared route[3][1]
route[0][1], route[0][2], route[1][1] ,[3][1] is now compared route[3][2]
route[0][1], route[0][2], route[1][1] ,[3][1] ,[3][2] is now compared to route [4][1] i.e [ now there is a match to route[1][1], so save the newcomer 'i' value and  break the 'i' loop

所以 i 值 [2 和 4] 是重复的,这是我的代码的预期结果。

4

2 回答 2

1

有什么反对索引零,零?

我也没有看到指针恶作剧的意义。

初始化所有数据是一般安全的事情。你知道,归零或什么的。

您在解决方案中建议的算法很难忠实于,但这会找到您的重复项。您必须在两个维度上遍历整个数组两次。

这也将匹配数据中的所有零,因此您可以添加一个例外来忽略路由值为零。

//Cycling through the array the first time.
for (i = 0; i < 6 ; i++)
{
    for (j = 0; j < 6; j++)
    {
        //Cycling through the array the second time
        for (x = 0; x < 6 ; x++)
        {
            for (y = 0; y < 6; y++)
            {
               if(i==x && j==y)
                   continue;
               if(routestore.route[i][j] == routestore.route[x][y])
                   printf("You have a match [%d][%d] =  [%d][%d]", i, j, x,y);
            }
        }
    }
}

好的,所以如果您只想查看一次匹配项,即 [0][2] == [2][1] 而不是 [2][1] == [0][2],那么您可以执行类似的操作我在下面有什么。这让我挠了挠头。通常,当它是一个简单的项目列表时,您将内部循环初始化为外部循环的值加一。但是当它是一个二维数组时,你不能完全做到这一点。所以我放弃了,做了一个超级蹩脚的黑客工作。我非常喜欢在可能的情况下进行暴力破解。我通常会告诉你不要使用这样的指针。

现在......如果你有三个相似的值,这仍然会有多次点击。如果这让您感到厌烦,那么您需要开始构建一个列表,并在您浏览数据时将命中与该列表进行比较。

#include <stdio.h>
#include <string.h>

struct route{
    int route[6][6];
    int no_routes_found;
    int count_each_route[6];
};

int lameAddOneAlternative(int *i, int *j)
{
  if((*j)<6)
  {
    (*j)++;
    return 1;
  }
  else if (*i<6)
  {
    (*i)++;
    (*j) = 0;
    return 1;
  }  
  return 0;
}

int main(int argc, char **argv)
{
  struct route routeStore;  
  int i,j,x,y;

  memset(routeStore.route,0,sizeof(int)*36);

  // the data
  routeStore.route[0][1] = 24;
  routeStore.route[0][2] = 18;
  routeStore.route[1][1] = 25;
  routeStore.route[2][1] = 18;
  routeStore.route[3][1] = 26;
  routeStore.route[3][2] = 19;
  routeStore.route[4][1] = 25;
  routeStore.route[4][2] = 84;

  //Cycling through the array the first time.
  for (i = 0; i < 6 ; i++)
  {
    for (j = 0; j < 6; j++)
    {
      x=i;
      y=j;
      //Cycling through the array the second time
      while(lameAddOneAlternative(&x,&y))
      {
        if(routeStore.route[i][j] == 0 )
          continue;
        if(routeStore.route[i][j] == routeStore.route[x][y])
          printf("You have a match [%d][%d], [%d][%d] == %d\n", i, j, x,y, routeStore.route[i][j] );

      }
    }
  }
}
于 2013-02-14T16:32:59.373 回答
0
for (i = 0; i <(route_ptr->no_routes_found) ; i++)
{
     for (j = 1; j < route_ptr-> count_each_route[i]; j++)
     {          
          for (x = 0; x < (route_ptr->no_routes_found) ; x++)
          {
               for (y = 0; y < route_ptr-> count_each_route[x]; y++)
               {
                  if(i==x && j==y)
                  continue;
                  if(route_ptr->route[i][j] == route_ptr->route[x][y])
                  printf("You have a match [%d][%d] =  [%d][%d]\n", i, j, x,y);
              }
         }     


    }
于 2013-02-14T19:43:50.673 回答