zoj 2132.The Most Frequent Number

The Most Frequent Number

#include<stdio.h>
#include<stdlib.h>
struct Node
{
	int e,n;
	Node *next;
};
int L;
Node* A;
int X;

int main()
{
	int e;
	Node* p,* q,* r;
	A=NULL;
	while(scanf("%d",&L)==1){
		scanf("%d",&e);
		A=(Node *)malloc(sizeof(Node));
		A->e=e;
		A->n=1;
		A->next=NULL;
		L--;
		while(L>0){
			p=A;
			scanf("%d",&e);
			while(p!=NULL){
				if(p->e==e){
					p->n++;
					break;
				}
				q=p;p=p->next;
			}
			if(p==NULL){
				r=(Node *)malloc(sizeof(Node));
				r->e=e;
				r->n=1;
				r->next=NULL;
				q->next=r;
			}
			L--;
		}
		int maxn=0;
		p=A;
		while(p!=NULL){
			if((p->n)>maxn){
				maxn=p->n;
				X=p->e; 
			}
			p=p->next;
		}
		printf("%d\n",X);
		
		//释放A占用的内存
		p=A;
		while(p){
			q=p;
			p=p->next;
			free(q);
		}
		A=NULL; 
	}
	return 0;
}
//此程序的缺陷:如果有两个最大频率的话,无法输出,再此没做改进
#include<stdio.h>
#include<stdlib.h>
typedef struct lnode
{
	int e;
	int len;
	struct lnode *next;
}lnode;
int main()
{
	int n,e,x;
	lnode *a,*p;
	lnode *q;
	lnode *a1,*a2;
	lnode *r,*s,*r1;
	a=NULL;
	p=NULL;
	printf("input n:");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		printf("please input no.%d:",i+1);
		scanf("%d",&e);
	    if(a==NULL)
		{
			a=(lnode*)malloc(sizeof(lnode));
			a->e=e;a->len=1;a->next=NULL;
		}
		else
		{
			a2=(lnode*)malloc(sizeof(lnode));
			a2->e=e;a2->len=1;a2->next=NULL;a->next=a2;a=a2;
		}
		if(p==NULL)
		{
			p=(lnode*)malloc(sizeof(lnode));
			p->e=e;p->next=NULL;p->len=1;
		}
		else
		{
			q=p;
			while(q)
			{
				if(q->e==e) 
				{
				q->len++;
				break;
				}
				s=q;q=q->next;
			}
			if(q==NULL)
			{
			  r1=(lnode*)malloc(sizeof(lnode));
			  r1->e=e;r1->next=NULL;r1->len=1;
			  s->next=r1;
			}
		}
	}
//	while(a)
//	{printf("%d",a->e);a=a->next;}
//	while(p)
//	{printf("%d %d",p->e,p->len);p=p->next;}
int max=0;
while(p)
{
	if(p->len>max)
	{
		max=p->len;
		x=p->e;
	}
	p=p->next;
}
printf("%d\n",x);
return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值