zoukankan      html  css  js  c++  java
  • HDU-1029-Ignatius aned the Princess IV

    链接:https://vjudge.net/problem/HDU-1029#author=0

    题意:

    给你n个数字,请你找出出现至少(n+1)/2次的数字。

    思路:

    dp,hash超时了,不知道是不是我的问题。

    cnt记录数量,

    目标数比其他数加起来还多。

    所以当cnt为0时当前数为目标数,相同cnt++,不同cnt--。

    代码:

    #include <iostream>
    #include <memory.h>
    #include <string>
    #include <istream>
    #include <sstream>
    #include <vector>
    #include <stack>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <math.h>
    #include <cstdio>
    using namespace std;
    
    typedef long long LL;
    
    const int MAXN = 1e5 + 10;
    const int INF = 0x7fffffff;
    
    int main()
    {
        int n;
        while (~scanf("%d", &n))
        //while (cin >> n)
        {
            int cnt = 0,a,vis;
            for (int i = 1;i <= n;i++)
            {
                scanf("%d", &a);
                if (cnt == 0)
                {
                    vis = a;
                    cnt++;
                }
                else
                {
                    if (a == vis)
                        cnt++;
                    else
                        cnt--;
                }
            }
            printf("%d
    ", vis);
        }
    
    
        return 0;
    }
    

      

  • 相关阅读:
    JSP——useBean
    jsp 几种跳转
    jsp MIME
    T-SQL之触发器(下)
    T-SQL之触发器
    T-SQL之存储过程
    T-SQL之游标
    T-SQL之自定义函数
    T-SQL之函数
    T-SQL复习心得(1)变量
  • 原文地址:https://www.cnblogs.com/YDDDD/p/10354613.html
Copyright © 2011-2022 走看看