Y

来源:互联网 发布:童年动画 知乎 编辑:程序博客网 时间:2024/04/25 09:06
"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.
#include <iostream>#include <cstdio>#include <cstring>#include<algorithm>#include<string>#include<map>using namespace std;int g[999999];int main(){    int T;    //freopen("1.txt","r",stdin);    while(scanf("%d",&T)!=EOF)    {        for(int i=0;i<T;i++)cin>>g[i];        sort(g,g+T);int flag=1;        for(int i=0;i<T;)        {           while(g[i]==g[i+1]&&i+1<T)            {flag++;i++;}           if(flag>=(T+1)/2){cout<<g[i]<<endl;break;}           else {i++;flag=1;}        }    }    return 0;}



"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.

"But what is the characteristic of the special integer?" Ignatius asks.

"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.

Can you find the special integer for Ignatius?
Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
Output
For each test case, you have to output only one line which contains the special number you have found.
Sample Input
51 3 2 3 3111 1 1 1 1 5 5 5 5 5 571 1 1 1 1 1 1
Sample Output
351 
给你N个数,要你找到出现次数大于等于(n+1)/2的元素;
分析;刚开始我建立一个数组,每次在输入一个新的元素是,先把前面已进入的元素遍历一遍,看即将要插入的元素是否已存在数组中,若以存在,将该元素的次数加一,
不存在就把此元素插入,并把该元素的次数初始化为1;但由于每次插入新的元素都要把之前的遍历一遍,所以效率不高,没通过;然后想到先把元素一次性输入,然后排序,这样大小相同的会聚在一起,然后我再去遍历寻找,看哪一个是符合目标的;
原创粉丝点击