zoukankan      html  css  js  c++  java
  • HDU 1029 Ignatius and the Princess IV(map应用)

    Ignatius and the Princess IV

                Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others)

                        Total Submission(s): 12233    Accepted Submission(s): 4881

    Problem Description
    "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?
     
    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
    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
     
     
     
    方法一:运用STL中map求解
     1 #include <stdio.h>
     2 #include <map>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     int N, nTemp;
     8     map<int, int> mapSeq;
     9     while(scanf("%d", &N) != EOF)
    10     {
    11         mapSeq.clear();
    12         while(N--)
    13         {
    14             scanf("%d", &nTemp);
    15             ++mapSeq[nTemp];
    16         }
    17         int max_num = -1, mark;
    18         for(map<int, int>::iterator it = mapSeq.begin(); it != mapSeq.end(); it++)
    19         {
    20             if(it->second > max_num)
    21             {
    22                 max_num = it->second;
    23                 mark = it->first;
    24             }
    25         }
    26         printf("%d\n", mark);
    27     }
    28     return 0;
    29 }


    方法二:参考<<编程之美>>中2.3章节<<寻找发帖水王>>解法

    参考文章链接:http://www.cnblogs.com/tgkx1054/archive/2012/11/29/2795549.html

     1 #include <stdio.h>
     2 
     3 int main()
     4 {
     5     int N, nTemp, nTimes, candidate;
     6     while(scanf("%d", &N) != EOF)
     7     {
     8         nTimes = 0;
     9         while(N--)
    10         {
    11             scanf("%d", &nTemp);
    12             if(nTimes == 0)
    13             {
    14                 candidate = nTemp;
    15                 nTimes = 1;
    16             }
    17             else
    18             {
    19                 if(candidate == nTemp)
    20                     nTimes++;
    21                 else
    22                     nTimes--;
    23             }
    24         }
    25         printf("%d\n", candidate);
    26     }
    27     return 0;
    28 }

    分析:如果每次删除的数不同,那么在剩余的数中,要找的那个数肯定还会超过剩余数的一半,这样通过不断缩小规模,
         到输入完成后,candidate中保存的就是我们要求的那个数。

  • 相关阅读:
    day21作业
    作业day20
    K8S架构图
    day20 类的继承
    课上随手记
    第一篇CEPH集群的学习
    day19 类的讲解
    Eureka工作原理
    局域网、以太网(标准以太网、工业以太网和实时以太网)与无线局域网
    无线局域网,有线局域网
  • 原文地址:https://www.cnblogs.com/Dreamcaihao/p/3107228.html
Copyright © 2011-2022 走看看