資料結構實驗:雜湊表
阿新 • • 發佈:2021-01-13
技術標籤:ACM
Description
在n個數中,找出出現次數最多那個數字,並且輸出出現的次數。如果有多個結果,輸出數字最小的那一個。
Input
單組資料,第一行數字n(1<=n<=100000)。
接下來有n個數字,每個數字不超過100000000
Output
出現次數最多的數字和次數。
Sample
Input
3
1 1 2
Output
1 2
Hint
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct node { int num,sum; struct node *next; } Node; int sumn,numn; Node* Insert(Node *p,int k) { if(p==NULL) { Node *q=(Node*)malloc(sizeof(Node)); q->num=k; q->sum=1; q->next=p; if(q->sum>sumn||(q->sum==sumn&&q->num<numn)) { sumn=q->sum; numn=q->num; } return q; } if(p->num==k) { p->sum++; if(p->sum>sumn||(p->sum==sumn&&p->num<numn)) { sumn=p->sum; numn=p->num; } } else p->next=Insert(p->next,k); return p; } int main() { int n,k; Node *a[10000]; sumn=numn=0; scanf("%d",&n); for(int i=0; i<10000; i++) a[i]=NULL; while(n--) { scanf("%d",&k); a[k%10000]=Insert(a[k%10000],k); } printf("%d %d",numn,sumn); return 0; }