C中数组的频率分布 [英] Frequency distribution in array in C

查看:96
本文介绍了C中数组的频率分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是指C语言



我有一个数组,我已经分类成一个频率数组。例如:



未分类= 1,2,1,1,4,2,2

排序= 1,1,1 ,2,2,2,4



所需功能:打印最常用的号码。例子:



输出:1,2



问题是我想要一种有效的输出方法最常见的数字,我已经完成了(采用排序[0]给我最频繁的数字)。但是,如果我有多个具有相同频率的数字,我似乎无法编写有效的代码来输出最常见的数字。我希望能够输出(按数字顺序)最频繁的数字,但是如果有多个具有相同频率输出它们也是。



< b>我尝试了什么:



This refers to C language

I have an array and I have sorted into a frequency array. Example:

not-sorted = 1,2,1,1,4,2,2
sorted = 1,1,1,2,2,2,4

Desired functionality: print the most frequent number. Examploe:

Output: 1,2

The problem is I want an efficient way to output the most frequent number and I have done it (taking sorted[0] gives me the most frequent number). However I can't seem to be able to write an efficient code to output the most frequent number if I have more than one number with the same frequency. I want to be able to output (in numerical order), the most frequent number but if there are more than one with the same frequency output them as well.

What I have tried:

int main()
{
 int unsorted[10] = {2,2,1,1,4,5,6,7,8,9};
 int bucket[10][5],buck[10],b[10];
 int i,j,k,l, n, counter, num,div,large,passes;
 n = 10;
 int arr[n];
 for (i=0; i<n; i++){
    arr[i] = unsorted[i];
 }

 div=1;
 num=0;
 large=arr[0];

 for(i=0 ; i<n ; i++)
 {
  if(arr[i] > large)
   {
    large = arr[i];
   }

  while(large > 0)
  {
   num++;
   large = large/10;
  }

  for(passes=0 ; passes<num ; passes++)
  {
   for(k=0 ; k<10 ; k++)
   {
    buck[k] = 0;
   }
   for(i=0 ; i<n  ;i++)
   {
    l = ((arr[i]/div)%10);
    bucket[l][buck[l]++] = arr[i];
   }

   i=0;
   for(k=0 ; k<10 ; k++)
   {
    for(j=0 ; j<buck[k] ; j++)
    {
     arr[i++] = bucket[k][j];
    }
   }
   div*=10;
  }
 }

printf("%d", arr[0]);
}

推荐答案

由于您已将它们按频率顺序排序,因此单次通过输出将执行您想要的操作。

第一个元素是最常见的:将它添加到输出中,并计算它们。这很简单 - 循环直到找到不同的东西!如果你有不同的东西,你知道它是第二个最常见的。所以使用第二个循环来计算它们。如果数字相同,则将其添加到输出中,将匹配值设置为新值,并计算这些值。

当数字不同时,您输出的所有值都相同频率。



但是这有作业的味道,所以我不会给你任何代码!



And说实话,这不是我做的方式:因为为了建立一个按频率排序的列表,你必须先计算每个值,我会在我使用时使用这些信息排序(或者更可能是排序)给你想要的数字。
Since you have sorted them into frequency order, a single pass through the output will do what you want.
The first element is the most frequent: add it to the output, and count them. That's easy - loop through until you find something different! When you have something different, you know it's the second most frequent. So use a second loop to count them. If the number is the same, add it to the output, set the "match" value to new value, and count those.
When the number is different, you have output all the values with the same frequency.

But this smells of homework, so I'll give you no code!

And to be honest, that isn't the way I'd do it: because in order to establish a "sorted-by-frequency" list in the first place you must have counted each value, I'd use that information while I was sorting (or more likely instead of sorting) to give you the numbers you wanted.


这篇关于C中数组的频率分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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