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

    题目
    "OK, you are not too bad, em... But you can never pass the next test." feng5166 says. 

    "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? 

    InputThe 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. 
    OutputFor each test case, you have to output only one line which contains the special number you have found. 
    Sample Input

    5
    1 3 2 3 3
    11
    1 1 1 1 1 5 5 5 5 5 5
    7
    1 1 1 1 1 1 1

    Sample Output

    3
    5
    1
     1 /*******************************************
     2  *    dp挑战二     相信自己!! 加油
     3  *  2018/8/15    19:45 
     4  *******************************************/
     5  
     6 /*******************************************
     7  * 这仿佛不用dp也能写的样子 
     8  *******************************************/
     9 #include<iostream>
    10 #include<map>
    11 using namespace std;
    12 int a[1000000];
    13 int main(){
    14      int n;
    15      while(cin>>n){
    16          int num=0;
    17          int ans;
    18          for(int i=0;i<n;i++){
    19              cin>>a[i];
    20          }
    21          for(int i=0;i<n;i++){
    22              if(num==0){
    23                  ans=a[i];
    24                  num++;
    25              }
    26              else if(a[i]==ans)num++;
    27              else num--;
    28          }
    29          cout<<ans<<endl;
    30      }
    31      return 0;
    32 }
  • 相关阅读:
    MySQL数据库设计规范
    Docker安装redis
    Go-用本地时间解析时间字符串
    Docker安装mysql
    docker安装es
    Json官网文档
    leetcode题目和解答集合
    76. 最小覆盖子串
    165. 比较版本号
    958. 二叉树的完全性检验
  • 原文地址:https://www.cnblogs.com/CuteAbacus/p/9487949.html
Copyright © 2011-2022 走看看