题目:?
?
#include <stdio.h>
#include <stdlib.h>
void Sort_Count()
{
int count = 0;
printf("请输入个数:\n");
scanf("%d", &count);
getchar();
if (!(count >= 1 && count <= 100))
{
printf("输入不合法!\n");
return;
}
//根据数据个数在堆上申请数组大小
int *data = (int*)malloc(sizeof(int)*count);
//录入数据
printf("请输入数据元素:\n");
for (int i = 0; i < count; i++)
{
scanf("%d", &data[i]);
}
//将每个数对应的个数记录
int *record = (int*)malloc(sizeof(int)*count);
for (int i = 0; i < count; i++)
{
record[i] = 1;
}
//排序
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
if (data[i]>data[j])
{
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
}
}
//删除重复数
for (int i = 0; i < count; i++)
{
int tag = 0;
for (int j = i + 1; j < count; j++)
{
if (data[i] == data[j])
{
++tag;
++record[i];
--record[j];
}
}
i += tag;
}
int max = 0;
//保存出现最大次数
for (int i = 0; i < count; i++)
{
if (record[i] > max)
{
max = record[i];
}
}
//打印次数最多
for (int i = 0; i < count; i++)
{
if (record[i] == max)
{
printf("%d %d\n", data[i], max);
}
}
free(data);
free(record);
data = NULL;
record = NULL;
}
int main()
{
Sort_Count();
system("pause");
return 0;
}
?
?
图解:?
举例:?
?
|