#### 解题思路:
①把众数集看做链表,每个结点包括:zhongshu(众数),chongshu(重数)
②:每输入一个数x,看它在众数集中是否存在
③:若不存在,链表中增加结点
④:若存在,zhongshu等于x的结点的chongshu加1
⑤:完成输入后遍历链表第一遍,找出最大的重数:max_chongshu
⑥:把链表中chongshu小于max_chongshu的结点删除(代码中为了少遍历,采取的方法最后一个结点没有遍历到,后面单独处理)
⑦:留下来的结点就是重数相同且重数最大
⑧:在剩下的节点中找到最小的众数,输出
#### 参考代码:
```c
#include #include typedef struct node_ {
int chongshu; //重数
double zhongshu;//众数
struct node_ *next;
}*node, Node;
/*-----------------------------------------------*/
int search( double x, node head );//查找众数x是否存在
void add_node( double x, node head );//添加众数x
void output( node head );//输出结果
/*-----------------------------------------------*/
int main()
{
int n;
double x;
Node Head;
Head.next = NULL;
scanf("%d",&n);
for ( int i = 0; i next;
while ( p != NULL )
{
if ( x == p->zhongshu ) //找到众数,已存在
{
p->chongshu++;
term = 1;
break;
}
p = p->next;
}
return(term);
}
/*-----------------------------------------------*/
void add_node( double x, node head )//添加众数x
{
node q = (node) malloc( sizeof(Node) );
q->chongshu = 1;
q->zhongshu = x;
q->next = head->next; //前插法
head->next = q;
return;
}
/*-----------------------------------------------*/
void output( node head )
{
node p = head->next;
if ( p == NULL ) //链表为空,则返回
return;
//求最大重数:max_chongshu
int max_chongshu = p->chongshu;
while ( p != NULL )
{
if ( max_chongshu chongshu )
max_chongshu = p->chongshu;
p = p->next;
}
/* delete删除 chongshu小于max_chongshu 的结点 ---------------------------------*/
//删除方法找一个删除一个,只遍历一遍,故指针q只指向被删除结点的前一个,最终尾结点没遍历到
node q = head;
node d = NULL;
while ( q->next != NULL )
{
if ( q->next->chongshu next;
q->next = q->next->next;
free( d );
}else
q = q->next;
}
/*-----------------------------*/
//判断尾结点chongshu,小于max_chongshu则删除
node h = head;
while ( h->next != q )
h = h->next;
if ( q->chongshu next = q->next;
free( q );
/*------------------------------*/
//在chongshu相同且为最大的节点中,找到最小的众数输出
q = head->next;
double min_zhongshu = q->zhongshu;
while ( q != NULL )
{
if ( q->zhongshu zhongshu;
q=q->next;
}
printf( "%.0lf\n", min_zhongshu );//输出众数
printf( "%d", max_chongshu );//输出重数
}
```
#### 解题思路2:
设输入的数字集为num{8,9,7,8,9,9,9,6,8,9,7,7,2,1,1,2,6}
从小到大排序后为num1{1,1,2,2,6,6,7,7,7,8,8,8,9,9,9,9,9}
显然发现众数为9,因为观察9最长,更具这个特点我们把题目转化为:给定一个数字集,如上mun1,其中数字满足从小到大的排序,求其中重复出现次数最多的数字,以及出现的次数。
#### 参考代码:代码思路来自1402:简单字符串
```c
#include#include#includeint comp(const void *value1, const void *value2) {
// 升序
return (*(int*)value1 - *(int*)value2);
}
int main()
{
int n,Startpos,Length,ii,jj;
int *num;
while(scanf("%d",&n)!=EOF)
{
num=(int *)malloc(n*sizeof(int));//开辟空间
for(int i=0;iLength)
{
Length=jj-ii+1;//更新众数重数
Startpos=ii;//更新众数下标
}
ii=i+1;//新的可能是众数的下标
}
}
printf("%d\n",num[Startpos]);
printf("%d\n",Length);
free(num);
}
}
```
0.0分
2 人评分