zoukankan      html  css  js  c++  java
  • bzoj2456mode

    bzoj2456mode

    题意:

    给你一个n个数的数列,求出现次数超过n div 2的数(只有1个)。

    题解:

    注意空间只有1M,显然不能开数组。用两个变量,一个存“当前数”,另一个存“当前数”的个数,如果读入的数与“当前数”相同就个数加一,如果不同就减一。如果个数减到0就换“当前数”为现在读入的数。因为如果那个“众数”个数超过ndiv2,所以一定不会被其他数抵消完,能够“坚持”到最后,所以答案就是最后的那个“当前数”。思路真巧。

    代码:

     1 #include <cstdio>
     2 using namespace std;
     3 int main(){
     4     int n,x,t,tot; tot=0; scanf("%d",&n);
     5     while(n--){
     6         scanf("%d",&x); 
     7         if(tot==0)t=x,tot=1;else if(x==t)tot++;else tot--;
     8     }
     9     printf("%d",t);
    10 }

    20160408

  • 相关阅读:
    emacs command
    emacs format
    attach
    虚拟ip
    emacs redo
    mariadb
    unsafe
    select, poll, epoll
    03基于python玩转人工智能最火框架之TensorFlow介绍
    03SQL语句
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/5697003.html
Copyright © 2011-2022 走看看