【问题标题】:Count the number of times a number appears in an array计算一个数字在数组中出现的次数
【发布时间】:2014-08-05 15:54:25
【问题描述】:

我正在开发一个小程序,用于计算整数在数组中出现的次数。 我设法做到了,但有一件事我无法克服。

我的代码是:

#include <stdio.h>

int count_occur(int a[], int num_elements, int value);
void print_array(int a[], int num_elements);

void main(void)
{
  int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
  int num_occ, i;

  printf("\nArray:\n");
  print_array(a, 20);

  for (i = 0; i<20; i++)
  {
    num_occ = count_occur(a, 20, a[i]);
    printf("The value %d was found %d times.\n", a[i], num_occ);
  }
}

int count_occur(int a[], int num_elements, int value)
/* checks array a for number of occurrances of value */
{
  int i, count = 0;
  for (i = 0; i<num_elements; i++)
  {
    if (a[i] == value)
    {
        ++count; /* it was found */
    }
  }
  return(count);
}

void print_array(int a[], int num_elements)
{
  int i;
  for (i = 0; i<num_elements; i++)
  {
    printf("%d ", a[i]);
  }
  printf("\n");
}

我的输出是:

Array:
2 5 0 5 5 66 3 78 -4 -56 2 66 -4 -4 2 0 66 17 17 -4 
The value 2 was found 3 times.
The value 5 was found 3 times.
The value 0 was found 2 times.
The value 5 was found 3 times.
The value 5 was found 3 times.
The value 66 was found 3 times.
The value 3 was found 1 times.
The value 78 was found 1 times.
The value -4 was found 4 times.
The value -56 was found 1 times.
The value 2 was found 3 times.
The value 66 was found 3 times.
The value -4 was found 4 times.
The value -4 was found 4 times.
The value 2 was found 3 times.
The value 0 was found 2 times.
The value 66 was found 3 times.
The value 17 was found 2 times.
The value 17 was found 2 times.
The value -4 was found 4 times.

如何避免输出中出现双行?

【问题讨论】:

  • 一种方法是保留已搜索的值列表
  • 不确定你的 C 有多好,但你可以复制数组然后对其进行排序。那么所有相等的值都是连续的,更容易计数,避免重复。
  • @DrewMcGowen 这应该足以作为答案
  • @DrewMcGowen 我知道这是这种方式,但我最后一次用 c 编程是在 3 年前。我忘记了如何使用动态数组..

标签: c arrays


【解决方案1】:

您可以使用并行数组,本示例使用char[20] 以节省一些空间:

#include <stdio.h>

int count_occur(int a[], char exists[], int num_elements, int value);
void print_array(int a[], int num_elements);

int main(void) /* int main(void), please */
{
    int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
    char exists[20] = {0}; /* initialize all elements to 0 */
    int num_occ, i;

    printf("\nArray:\n");
    print_array(a, 20);

    for (i = 0; i < 20; i++)
    {
        num_occ = count_occur(a, exists, 20, a[i]);
        if (num_occ) {
            exists[i] = 1; /* first time, set to 1 */
            printf("The value %d was found %d times.\n", a[i], num_occ);
        }
    }
}

int count_occur(int a[], char exists[], int num_elements, int value)
/* checks array a for number of occurrances of value */
{
    int i, count = 0;

    for (i = 0; i < num_elements; i++)
    {
        if (a[i] == value)
        {
            if (exists[i] != 0) return 0;
            ++count; /* it was found */
        }
    }
    return (count);
}

void print_array(int a[], int num_elements)
{
    int i;
    for (i = 0; i<num_elements; i++)
    {
        printf("%d ", a[i]);
    }
    printf("\n");
}

此方法更快,因为它会跳过已读取的值并从count_ocurr 中的i 开始迭代:

#include <stdio.h>

int count_occur(int a[], char map[], int num_elements, int start);
void print_array(int a[], int num_elements);

int main(void)
{
    int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
    char map[20] = {0};
    int num_occ, i;

    printf("\nArray:\n");
    print_array(a, 20);

    for (i = 0; i < 20; i++)
    {
        if (map[i] == 0) {
            num_occ = count_occur(a, map, 20, i);
            printf("The value %d was found %d times.\n", a[i], num_occ);
        }
    }
}

int count_occur(int a[], char map[], int num_elements, int start)
/* checks array a for number of occurrances of value */
{
    int i, count = 0, value = a[start];

    for (i = start; i < num_elements; i++)
    {
        if (a[i] == value)
        {
            map[i] = 1;
            ++count; /* it was found */
        }
    }
    return (count);
}

void print_array(int a[], int num_elements)
{
    int i;
    for (i = 0; i< num_elements; i++)
    {
        printf("%d ", a[i]);
    }
    printf("\n");
}

【讨论】:

    【解决方案2】:

    如果当前索引也是相关数字第一次出现的索引,我建议仅打印该语句。

    count_occur 中,您拥有 i 中每个匹配项的索引。如果您将imain 传递到count_occur,则如果该值大于count_occur 中的i,则可以执行诸如返回-1 之类的操作。然后,如果你在main 中得到 -1,请不要打印。

    此外,您的算法可以更快。您可以对数组的副本进行排序,以便高效地完成搜索,而不是每次都线性搜索数组。 (即使您使用一个数组来索引而另一个来搜索,它会更快 - 并且仍然以相同的顺序返回值。)

    【讨论】:

    • 我不允许对这个进行排序。
    • 如果无法修改源数组,则改为对数组的副本进行排序。
    • 这不是问题。问题是输出必须与数组中出现的数字顺序相同。
    • 您没有指定。但无论如何,这个答案的第一部分仍然适用。
    【解决方案3】:
    #include <stdio.h>
    #include <stdbool.h>
    #include <string.h>
    
    int count_occur(int a[], int num_elements, int value, bool selected[]);
    void print_array(int a[], int num_elements);
    
    int main(void){
        int a[] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
        int size = sizeof(a)/sizeof(*a);
        bool ba[size];
        memset(ba, 0, sizeof ba);
        int num_occ, i;
    
        printf("\nArray:\n");
        print_array(a, size);
    
        for (i = 0; i<size; i++){
            if(ba[i] == true) continue;//skip already count
            num_occ = count_occur(a, 20, a[i], ba);
            printf("The value %d was found %d times.\n", a[i], num_occ);
        }
    }
    
    int count_occur(int a[], int num_elements, int value, bool ba[]){
        int i, count = 0;
        for (i = 0; i<num_elements; i++){
            if (a[i] == value){
                ba[i] = true;
                ++count;
            }
        }
        return count;
    }
    
    void print_array(int a[], int num_elements){
        int i;
        for (i = 0; i<num_elements; i++){
            printf("%d ", a[i]);
        }
        printf("\n");
    }
    

    小改进

    int count_occur(int a[], int num_elements, int index, bool selected[]);
    
    num_occ = count_occur(a, 20, i, ba);
    
    int count_occur(int a[], int num_elements, int index, bool ba[]){
        int i, count = 0;
        for (i = index; i<num_elements; i++){
            if (a[i] == a[index]){
                ba[i] = true;
                ++count;
            }
        }
        return count;
    }
    

    【讨论】:

      【解决方案4】:
      #include<stdio.h>
      #include<string.h>
      
      int main()
      {
        int arr[] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
        int arrSize = sizeof(arr)/sizeof(arr[0]);
        int tracker[20];
        int i,j,k=0,l=0,count,exists=0;
      
        for (i=0;i<arrSize;i++)
          printf("%d\t", arr[i]);
        printf("\n");
      
        memset(tracker, '$', 20);
      
        for (i=0, j=i+1, count=1, l=0; i<arrSize; i++)
        {
          j=i+1;
          count=1;
          l=0;
      
          while (l < arrSize)
          {
            if (arr[i] == tracker[l])
            {
              exists = 1;
              break;
            }
            l++;
          }
      
          if (1 == exists)
          {
            exists = 0;
            continue;
          }
      
          while (j < arrSize)
          {
            if (arr[i] == arr[j])
              count++;
            j++;
          }
          tracker[k] = arr[i];
          k++;
      
          printf("count of element %d is %d\n", arr[i], count);
        }
      
      }
      

      【讨论】:

      • 我使用跟踪器阵列的解决方案...希望对您有所帮助!
      【解决方案5】:
      very simple logic to count how many time a digit apper
      #include<stdio.h>
        int main()
      {
      int a,b,c,k[10];
      int p[10]={0};
      int bb[10]={0};
      scanf("%d\n",&a);
      for(b=0;b<a;b++)
      {
          scanf("%d",&k[b]);
      
      }
      for(b=a-1;b>0;b--)
      {
          for(c=b-1;c>=0;c--)
          {
              if((k[b]==k[c])&&(bb[c]==0))
              {
                  p[b]=p[b]+1;
                  bb[c]=1;
              }
          }
      }
          for(c=0;c<a;c++)
              {
              if(p[c]!=0)
              {
                  printf("%d is coming %d times\n",k[c],p[c]+1);
              }
          }
          return 0;
      }
      

      【讨论】:

        【解决方案6】:

        在你的功能中:

        int count_occur(int a[], int num_elements, int value)
        /* checks array a for number of occurrances of value */
        {
          int i, count = 0;
          for (i = 0; i<num_elements; i++)
          {
            if (a[i] == value)
            {
                ++count; /* it was found */
                a[i] = INFINITY;              // you can typedef INFINITY with some big number out of your bound
            }
          }  
          return(count);
        }
        

        在 main() 中你可以编辑 for 循环:

        for (i = 0; i<20; i++)
          {
             if(a[i] != INFINITY)
             {
                 num_occ = count_occur(a, 20, a[i]);
                 printf("The value %d was found %d times.\n", a[i], num_occ);
             }
          }
        

        【讨论】:

          【解决方案7】:

          可以用两个数组解决

          #include <stdio.h>
          
          
          
          int main() {
          
          int array[12] = {1,2,2,5,2,5,7,6,2,4,2,4}; 
          
          int array2[100] = {0};
          
          int indicator = 1;
          
          int i = 0,j;
          
          int index = 0;
          
          int number_count;
          
          for(int i = 0; i<12;i++) {
              indicator = 1;
          
              for(j = i+1;j<12;j++) {
          
                  if(array[i] == array[j]){
          
                      indicator = -1;
          
                      break;
                  }
          
              }
          
              if(indicator == 1){
          
                  array2[index] = array[i];
                  index++;
              }
          }
          
          for(int k = 0; array2[k]; k++) {
          
          number_count = 0;
          
          for(int m = 0; m<12;m++){
          
              if(array2[k] == array[m]){
          
                  number_count++;
              }
          }
          
          printf("%d was found %d times...\n",array2[k],number_count);
          }
          
          
          
          return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-02
            • 2022-01-11
            • 1970-01-01
            相关资源
            最近更新 更多