【问题标题】:Searching an Array in C在 C 中搜索数组
【发布时间】:2015-10-20 08:16:11
【问题描述】:

我很难进行这样的练习:

编写一个程序,计算数组中每个不同数字的出现次数。整数是 0 - 9 之间的数字。以这个数组为例:

int numbers[] = {1, 5, 4, 7, 1, 4, 1, 7, 5, 5, 3, 1};

创建一个指向数组第一个元素的指针。循环遍历数组并计算指向数字的出现次数。数完数字 1 的出现次数后,指针会指向数字 5。计算 5 的出现次数,以此类推。在您循环了几次并且指针再次指向 1(数组的元素 4)之后,程序可能不会再数 1,因此您需要将分数保持在您已经数过的数字的某个位置。 em>

输出应该类似于:

1的出现次数为:4

3的出现次数为:1

4的出现次数为:2

5的出现次数为:3

7的出现次数为:2

我现在的代码计算了每个元素的外观:

int main()
{   
    int getallen[] = {1, 5, 4, 7, 1, 4, 1, 7, 5, 5, 3, 1};
    int i , j, *aimedNumber, appearance = 0, arraySize = sizeof(getallen) / sizeof(int);

    for(i=0;i<arraySize;i++)                        
    {
        aimedNumber = getallen[i];                  // every loop, the pointer points to the next element

        for(j=0; j<arraySize; j++)                  // loops through the array comparing the pointer
        {
            if(getallen[j]==aimedNumber)            // if the element of the array == pointer
            {
                appearance++;                       // +1 to the appearance
            }
        }

        printf("the appearance of %i in the array is: %i\n", aimedNumber, appearance);
        appearance = 0;                             // after checking the appearance of the pointed number...
    }                                               // reset the appearance variable

    return 0;
}

但我仍然需要检查我是否已经数过一个数字,如果我已经数过了,请确保该数字不会再次被计算。

提前致谢!

【问题讨论】:

  • 如果你可以使用set,那么你的问题就解决了,但它是stl和c++
  • 你的外循环应该运行在所有可能的数字上,即在你的情况下从 0 到 9,并且aimedNumber 等于 ´i`。 (当然,如果你知道可能值的范围,你可以只遍历内部循环一次,并为每个从 0 到 9 的数字填充一个计数数组。)
  • 好吧,您并没有在每次传递中减少数组中的数字,而是从 0 开始 j 。如果您数 1,请确保您的下一个数组从 5、4、7、4 开始...

标签: c arrays pointers search


【解决方案1】:

数组中的数字只能介于 0 和 9(含)之间,这实际上使它更简单,因为这样您就可以拥有一个由十个整数组成的“计数器”数组(每个整数一个您“搜索”的数组中的数字),并且对于主数组中的每个数字,您增加计数器数组中的相应值(使用数字作为索引)。然后简单地打印出计数器数组中非零的值。

类似下面的东西

int count[10] = { 0 };  // Initialize all to zero

for (i = 0; i < arraySize; ++i)
    ++count[getallen[i]];

// Now print out all non-zero values from count,
// and the index is the number in getallen

【讨论】:

  • 非常感谢,这种方式也可以,但是分配说必须使用指针。但无论如何,谢谢我也从中学到了很多东西!
【解决方案2】:

您的程序应如下所示:

#include <stdio.h>

int main(void)
{
    //Original array of numbers
    int numbers[] = {1, 5, 4, 7, 1, 4, 1, 7, 5, 5, 3, 1};

    //array of 10 integers to count the number of occurences
    int num_of_occ[10] = {0};

    int *ptr = numbers;

    //tmp variable to hold the value of numbers array in the loop
    int tmp ,n = 0 ;

    while( n < 12 )
    {
        tmp = *ptr;
        num_of_occ[tmp]++;
        ptr++;
        n++;
    }

    //print the occurences
    for( int n = 0 ; n < 10 ; n++ )
    {
        if( num_of_occ[n] != 0 )
        {
            printf("The ammount of appearences of %d : %d\n", n , num_of_occ[n]);
        }
    }
}

【讨论】:

  • 是的!谢谢你!这样你仍然可以使用指针(正如分配所说的那样)并且你得到正确的输出!
  • 非常感谢@machine_1!我仔细看了一下编辑,真是太棒了!
【解决方案3】:

由于您在计数后似乎不需要getallen 数组,因此您可以在计数时对其进行修改,即当您在数组中查找所有1s 时,每次读取1你可以将它设置为,比如说,-1(你知道你所有的号码都在[0,9]范围内),这样当你回到外循环时,你可以跳过值为-1

编辑:如果您必须遵循示例中描述的那种行为,这就是答案;否则,像 Joachim Pileborg 的那样,带有额外数组的解决方案会更好。

【讨论】:

    【解决方案4】:

    您需要创建另一个名为 tempArr 的数组,例如 getallen 数组的大小,用 10 对其进行初始化(因为 getallen 数组中的数字在 0-9 之间),然后在计算下一个数字之前,扫描 tempArr 并检查是否该号码已经存在,如果存在,请跳至下一个号码。

    【讨论】:

      【解决方案5】:

      我只是在你设置了目标编号之后再进入另一个循环,但在你进入内部循环以检查数组的先前值与目标编号之前

      skip = 0;
      for (j=0; j < i; j++) {
        if (getallen[j] == aimedNumber) {
          skip = 1;
          break;
        }
      }
      if (skip) {
        continue;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-08
        • 1970-01-01
        • 2013-05-28
        • 1970-01-01
        • 2017-10-20
        • 2013-12-26
        • 2013-04-30
        • 1970-01-01
        相关资源
        最近更新 更多