数据结构实验:哈希表
生活随笔
收集整理的這篇文章主要介紹了
数据结构实验:哈希表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據結構實驗:哈希表
題目描述
在n個數中,找出出現次數最多那個數字,并且輸出出現的次數。如果有多個結果,輸出數字最小的那一個。輸入
單組數據,第一行數字n(1<=n<=100000)。接下來有n個數字,每個數字不超過100000000
輸出
出現次數最多的數字和次數。示例輸入
3 1 1 2示例輸出
1 2 #include<stdio.h> #include<stdlib.h> struct node {int data;int ans;struct node *next; }*head[100010]; int main() {int i, n, x, y, shu = -1, ci = -1;struct node *p, *q, *r;for(i=0; i<100000; i++) {head[i] = (struct node *)malloc(sizeof(struct node));head[i]->next = NULL;}scanf("%d", &n);for(i=0; i<n; i++) {scanf("%d", &x);y = x % 100000;p = head[y]->next;q = head[y];while(p != NULL) {if(p->data == x) {p->ans++;if(ci < p->ans) {shu = p->data;ci = p->ans;}else if(ci == p->ans) {if(shu > p->data)shu = p->data;}break;}q = p;p = p->next;}if(p == NULL) {r = (struct node *)malloc(sizeof(struct node));r->data = x;r->ans = 1;if(ci < r->ans) {shu = r->data;ci = r->ans;}else if(ci == r->ans) {if(shu > r->data)shu = r->data;}q->next = r;r->next = NULL;}}printf("%d %d\n", shu, ci);return 0; }轉載于:https://www.cnblogs.com/Genesis2018/p/8304784.html
總結
以上是生活随笔為你收集整理的数据结构实验:哈希表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LigerUi之Grid使用详解(二)—
- 下一篇: LinearLayout和Relativ