求众数(c语言)

众数是指一组数据中,出现次数最多的数叫众数

程序思路:先将数据从大到小排序,然后把重复出现的数和出现的次数存放在一个二维数组里边,再通过查找出现次数找到最大的,即出现次数最多的,即为众数。

#include 
#include 
#include 

void sort(int *p,int length)//冒泡,从大到小排序
{
	int i=0,j=0;
	int temp[128];
	int num_sort[128];
	int len;
	int max;
	
	memset(temp,0,sizeof(temp));
	
	len = length;
	memcpy(temp,p,len*sizeof(int));
		
	for(i=0;i
	max = buffer[1][0];
	count = 0;
	for(i=0;i max)
		{
		   max = buffer[1][i];
		   count = i;
		}
	}
		
	printf("zhongshu is :");
	for(i=0;i	}
	printf("\n");

	free((void *)buffer[0]);
	free((void *)buffer[1]);
		
}

int main(int argc,char *argv[])
{
	int i = 0;
	int buffer[128];
	memset(buffer,0,sizeof(buffer));
	
	int len;
	
	len = argc-1;
	
	for(i=0;i

例如,输入1 9 8 4 5 8 1 9 0 10 10 10 17 18 19 19 19 19 10

输出打印为:

buffer [0] is 19
buffer [1] is 19
buffer [2] is 19
buffer [3] is 19
buffer [4] is 18
buffer [5] is 17
buffer [6] is 10
buffer [7] is 10
buffer [8] is 10
buffer [9] is 10
buffer [10] is 9
buffer [11] is 9
buffer [12] is 8
buffer [13] is 8
buffer [14] is 5
buffer [15] is 4
buffer [16] is 1
buffer [17] is 1
buffer [18] is 0
zhongshu is :19,10,




你可能感兴趣的:(CC++)