有趣的题目

空间1mb,所以开数组的算法就不要想了(我一开始没看到……)

仔细读题,然后发现这里他限定众数为出现超过n div 2次

也就是说,这个数可以对应每一个不相同的数消掉,最终还剩下这个数

也就是说,我们遍历,遇到不相同的就两两抵消,最终剩下的一定是ans

得解,

 1 var n,i,s,ans,x:longint;
 2 begin
 3   readln(n);
 4   s:=0;
 5   for i:=1 to n do
 6   begin
 7     read(x);
 8     if (s=0) then
 9     begin
10       inc(s);
11       ans:=x;
12     end
13     else if ans=x then inc(s)
14     else dec(s);
15   end;
16   writeln(ans);
17 end.
View Code

我需要锻炼思维能力,恩恩

 

posted on 2014-05-02 23:59  acphile  阅读(131)  评论(0编辑  收藏  举报