发现在一个二维阵列的重复的元素 [英] Find the duplicate elements in a two-dimensional array

查看:129
本文介绍了发现在一个二维阵列的重复的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to find the duplicate elements in a two dimensional array.

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;

以上是我的数据;路由的重复项[2] [1](路由重复[0] [2])和路线[4] [1](路由重复[1] [1])已被发现。

Those are my data; the duplicate entries of route[2][1] (duplicate of route[0][2]) and route[4][1] (duplicate of route[1][1]) has to be found.

解决方案是重复的'我'的路线值[I] [J],这是2及4从这个例子。

The solution is the duplicate 'i' value of route[i][j] which is 2 & 4 from this example.

请指导我。

#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]);
        }
    }
}

预期的解决方案是:

The solution expected is:

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

所以,我值[2,4]是重复的,那就是我的code我预期的结果。

So i values [2 and 4] are duplicate, and that is my expected result of my code.

推荐答案

要反对指数为零,零的东西吗?

Got something against index zero, zero?

我还没有看到指针有心计的地步。

I also don't see the point of the pointer shenanigans.

这是一个一般安全事来初始化所有数据。要知道,零什么的。

It's a general safety thing to initialize all your data. You know, to zero or something.

您在您的解决方案建议的算法是相当困难忠于,但这会发现你的副本。你必须通过整个数组走,在这两个方面,两次。

The algorithm you suggest in your solution is rather hard to be faithful to, but this will find your duplicates. You have to walk through the entire array, in both dimensions, twice.

这也将匹配您的数据全部为零,所以你可以添加一个例外忽略零路线值。

This will also match all the zeroes in your data, so you could add an exception to ignore routes values of zero.

//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],那么你就可以做一些像我下面。这一次让我挠我的头。一般,当它的项目的简单列表,则初始化内环到外环,加一的值。但你不能完全做到这一点时,它是一个二维数组。于是,我放弃了,并提出了超级跛脚下锅的工作。我是野蛮的强制事情尽可能的大风扇。我通常会告诉你不要使用这样的指针。

Ok, so if you only want to see matches once, ie [0][2] == [2][1] but not [2][1] == [0][2], then you can do something like what I have below. This one made me scratch my head. Usually, when it's a simple list of items, you initialize the inner loop to the value of the outer loop, plus one. But you can't quite do that when it's a 2D array. So I gave up and made a super-lame hack-job. I'm a big fan of brute forcing things when possible. I'd normally tell you not to use pointers like this.

现在...这仍会有多次点击,如果你有三个相似的价值观。如果你厌烦,那么你需要开始建立一个列表,当你穿行数据免受比较命中。

Now... this will still have multiple hits if you have three similar values. If that irks you then you need to start building a list and comparing hits against that as you walk through the data.

#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] );

      }
    }
  }
}

这篇关于发现在一个二维阵列的重复的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆