c数组问题

发布于 2024-11-04 18:58:24 字数 881 浏览 0 评论 0原文

我有一个像这样的数组:

int sayilar[10] = {5,6,2,1,4,2,5,5,5,2};

现在我想得到这样的结果:找到 2 个重复项,5= 4 次,2= 3 次。

不知道该怎么做。这是我的代码,它不能正常工作:

#include <stdio.h>

int main()
{
    int sayilar[10]={5,6,2,1,4,2,5,5,5,2};
    int i,j;
    int matris[5][2];

    int ar[5];

    int temp=0;
    int tempX;
    int k=0;

    for(i=0; i<10; i++)
    {
        for(j=i+1; j<10; j++)
        {
            if(sayilar[j]==sayilar[i])
            {
                if(temp==0)
                {
                    matris[k][0] = sayilar[j];
                    matris[k][1] = 1;
                    temp=1;
                } else
                {
                    matris[k][1]++;
                }
            }
        }
        if(temp!=0)
        k++;
        temp=0;
    }

    printf("%d %d",matris[0][0],matris[0][1]+1);
}

I have an array like this:

int sayilar[10] = {5,6,2,1,4,2,5,5,5,2};

now I want to get a result like: 2 duplicates found, 5= 4 times, 2= 3times.

Couldn't figure how to do that. Here is my code, that's not working right:

#include <stdio.h>

int main()
{
    int sayilar[10]={5,6,2,1,4,2,5,5,5,2};
    int i,j;
    int matris[5][2];

    int ar[5];

    int temp=0;
    int tempX;
    int k=0;

    for(i=0; i<10; i++)
    {
        for(j=i+1; j<10; j++)
        {
            if(sayilar[j]==sayilar[i])
            {
                if(temp==0)
                {
                    matris[k][0] = sayilar[j];
                    matris[k][1] = 1;
                    temp=1;
                } else
                {
                    matris[k][1]++;
                }
            }
        }
        if(temp!=0)
        k++;
        temp=0;
    }

    printf("%d %d",matris[0][0],matris[0][1]+1);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忘年祭陌 2024-11-11 18:58:24

你知道数组中的数字吗?

例如,如果您知道它们都在 1 到 10 之间,那么您可以创建一个大小为 10 的数组,其中包含每个数字的计数。

像这样的东西(未经测试)

int sayilar[] = {5,6,2,1,4,2,5,5,5,2};
int counts[10] = {};

for( int i=0; i<10; ++i)
{
    ++counts[sayilar[i]-1];
}

// Now the 'counts' array has:
// counts[0] == 1     - indicating one '1' found
// counts[1] == 3     - indicating three '2' found
// counts[2] == 0     - indicating zero '3' found
// etc.

如果您对 sayilar 中可能包含哪些值没有任何保证,那么首先排序可能是最好的选择,正如其他人提到的那样。查看 qsort,它将就地对数组进行排序。

Do you know anything about the numbers in the array?

If you know, for instance, that they will all be between 1 and 10, then you could just create an array of size 10 which would contain the count for each number.

Something like this (not tested)

int sayilar[] = {5,6,2,1,4,2,5,5,5,2};
int counts[10] = {};

for( int i=0; i<10; ++i)
{
    ++counts[sayilar[i]-1];
}

// Now the 'counts' array has:
// counts[0] == 1     - indicating one '1' found
// counts[1] == 3     - indicating three '2' found
// counts[2] == 0     - indicating zero '3' found
// etc.

If you don't have any guarantees about what values might be in sayilar, then sorting first is probably the best option, as others have mentioned. Check out qsort, which will sort your array in-place.

々眼睛长脚气 2024-11-11 18:58:24

我认为您应该在执行嵌套循环之前检查 sayilar[i] 不在 matris 中。

for(i=0; i<10; i++)
{
    int found = 0;
    for (int l=0; l<k; l++)
    {
       if (matris[l][0] == sayilar[i]) { found = 1; break; }
    }

    if (!found) 
    {
        for(j=i+1; j<10; j++)
        {
            //...

如果您想要更高级的解决方案,您可以对数组进行排序,其复杂度为 O(nlogn),而不仅仅是简单地迭代排序后的数组...只是为了获得灵感。

I think you should check that sayilar[i] is not in matris before doing the nested loop.

for(i=0; i<10; i++)
{
    int found = 0;
    for (int l=0; l<k; l++)
    {
       if (matris[l][0] == sayilar[i]) { found = 1; break; }
    }

    if (!found) 
    {
        for(j=i+1; j<10; j++)
        {
            //...

If you want more advanced solution you can sort the array, which has O(nlogn) complexity, and than just simply iterate thru the sorted array... Just for inspiration.

方圜几里 2024-11-11 18:58:24

理想情况下,您希望一次性完成此操作,而不是使用嵌套循环。最好的方法是使用某种映射结构,其中映射键是您正在计数的值,映射值是该值出现的次数。

对于这个特定的示例,最简单的方法是创建一个一维数组,其中索引对应于您正在计算的值。示例:

int sayilar[10]={5,6,2,1,4,2,5,5,5,2};  
size_t counts[10] = {0};
size_t dups = 0;
...
for (i = 0; i < 10; i++)
{
  /**
   * Add one to the value of counts[k], where k == sayilar[i]
   */
  counts[sayilar[i]]++;

  /**
   * If the count is equal to 2, add one to the value
   * of dups.  We use == 2 instead of > 1 so that we
   * only count unique duplicates.  
   */
  if (counts[sayilar[i]] == 2)
  {
    dups++;
  }
}

因此,当此循环执行时,counts 会更新如下:

counts[5] = 1;
counts[6] = 1;
counts[2] = 1;
counts[1] = 1;
counts[4] = 1;
counts[2] = 2; dups = 1;
counts[5] = 2; dups = 2;
counts[5] = 3;
counts[5] = 4;
counts[2] = 3;

不幸的是,如果您要跟踪非常广泛的值或非整数的值,则此方法的扩展性不太好。

C++ 和 Java 等语言提供了内置的地图数据类型,该数据类型通常构建在某种平衡树结构(例如红黑树)之上,对于这个特定问题来说,这超出了杀伤力。

Ideally, you'd like to do this in one pass, rather than using the nested loops. The best way is to use some sort of mapping structure, where the map key is the value you're counting, and the map value is the number of occurences for that value.

For this specific example, the easiest thing to do would be to create a single-dimensioned array, where the index corresponds to the value you're counting. Example:

int sayilar[10]={5,6,2,1,4,2,5,5,5,2};  
size_t counts[10] = {0};
size_t dups = 0;
...
for (i = 0; i < 10; i++)
{
  /**
   * Add one to the value of counts[k], where k == sayilar[i]
   */
  counts[sayilar[i]]++;

  /**
   * If the count is equal to 2, add one to the value
   * of dups.  We use == 2 instead of > 1 so that we
   * only count unique duplicates.  
   */
  if (counts[sayilar[i]] == 2)
  {
    dups++;
  }
}

So as this loop executes, counts gets updated as follows:

counts[5] = 1;
counts[6] = 1;
counts[2] = 1;
counts[1] = 1;
counts[4] = 1;
counts[2] = 2; dups = 1;
counts[5] = 2; dups = 2;
counts[5] = 3;
counts[5] = 4;
counts[2] = 3;

Unfortunately, this approach doesn't scale very well if you're tracking a very wide range of values, or values that aren't integers.

Languages like C++ and Java provide a built-in map data type which is usually built on top of some sort of balanced tree structure like a Red-Black tree, which is beyond overkill for this particular problem.

不及他 2024-11-11 18:58:24

如果我来写,我会分两个阶段来写。

首先,我将循环遍历数组,并计算每个数组的实例数:

int counts[5] = { 0,0,0,0,0 };

for ( int i=0 ; i < 10 ; ++i ) {
   counts[ sayilar[i] - 1 ] += 1;
}

接下来,我将循环并查找重复项:

for( int i=0 ; i < 5 ; ++i ) {
   if ( counts[i] > 1 ) {
      printf("Duplicate found: %d with %d instances\n", i+1, counts[i] );
   }
}

这种方法使其更具可读性。

If I were writing it, I'd do it in two stages.

First, I'd loop over the array, and count instances of each:

int counts[5] = { 0,0,0,0,0 };

for ( int i=0 ; i < 10 ; ++i ) {
   counts[ sayilar[i] - 1 ] += 1;
}

Next, I would loop over and look for duplicates:

for( int i=0 ; i < 5 ; ++i ) {
   if ( counts[i] > 1 ) {
      printf("Duplicate found: %d with %d instances\n", i+1, counts[i] );
   }
}

This approach leaves it much more readable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文