日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2:找众数

發布時間:2025/6/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2:找众数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
總時間限制:?
10000ms?
內存限制:?
4096kB
描述

一天,THU boy 小胖濤給一道題給zzh做:給你一個n個數的數列,其中某個數出現了超過n div 2次即眾數,請你找出那個數。

zzh:這不是很簡單嗎……等等,好像有什么不對。臥槽,我好像不會做啊!

你能幫助zzh嗎?


輸入
第一行:一個正整數 n

第二行:n個小于等于2^{31}-1的正整數
輸出
一個整數,表示你所找那個眾數。
樣例輸入
7 4 1 4 2 4 3 4
樣例輸出
4
提示
n<=500000,數列中每一個數<=2^31-1
注意看空間限制

c++的頭文件也會占用一定空間



#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<ctime>#include<cmath>#include<string>#define N 12350#define MAX 12345#define MAXSUM 12500000#define CLR(arr, what) memset(arr, what, sizeof(arr))using namespace std;int countmax;template<class T>class Hash{private:int Key[N], Head[N], Next[N], Same[N];int top;public:int search( int x);void push(int x);bool pre(int x);void clear();};template<class T>inline void Hash<T>::clear(){top = 0;CLR(Head, -1);CLR(Next, -1);CLR(Same, 0);}template<class T>inline bool Hash<T>::pre(int x){int temp;temp = abs(x) % MAX;for(int i = Head[temp]; i != -1; i = Next[i]) //記錄重復{if(x == Key[i]){Same[i]++;return true;}}return false;}template<class T>inline void Hash<T>::push(int x){if(pre(x) == true) //出現過,Same記錄return ;else //沒出現過{int temp;temp = abs(x) % MAX;Key[top] = x;Next[top] = Head[temp];Head[temp] = top;Same[top] = 1;top++;}}template<class T>inline int Hash<T>::search(int x){int temp;temp = abs(x) % MAX;for(int i = Head[temp]; i != -1; i = Next[i]){if(x == Key[i]){if(Same[i]>countmax){countmax=Same[i]; //遍歷same 找最大的same}}}return countmax; // 返回最大same}int main(){int m[100005];Hash<int> h;h.clear();int n;cin>>n;for(int i=0;i<n;i++){int j;cin>>j;m[i]=j;h.push(j);}//建表for(int i=0;i<n;i++){h.search(m[i]);} cout<<countmax; //輸出最大samereturn 0;}

總結

以上是生活随笔為你收集整理的2:找众数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。