HDOJ 1029 Ignatius and the Princess IV

1 /*Description:
2 Ignatius and the Princess IV
3 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 2048/1024 K (Java/Others)
4 Total Submission(s): 4142 Accepted Submission(s): 1242
5
6
7 Problem Description
8 "OK, you are not too bad, em... But you can never pass the next test." feng5166 says.
9
10 "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.
11
12 "But what is the characteristic of the special integer?" Ignatius asks.
13
14 "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.
15
16 Can you find the special integer for Ignatius?
17
18
19
20 Input
21 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.
22
23
24
25 Output
26 For each test case, you have to output only one line which contains the special number you have found.
27
28
29
30 Sample Input
31 5
32 1 3 2 3 3
33 11
34 1 1 1 1 1 5 5 5 5 5 5
35 7
36 1 1 1 1 1 1 1
37
38
39 Sample Output
40 3
41 5
42 1
43
44 *
45
46 在奇数个数中找到重复一半以上的数
47 输入数据 1.必须全为奇数 不必考虑0 负数
48 2.必有一个数是重复出现一半以上的
49 3.数据量大 卡内存 属于大数据卡内存的题目
50
51 设一个结果变量res 并用cnt来筛选 最后的结果是能使cnt不为零的数
52
53 Accepted 1029 218MS 208K
54
55 * Author: Vincent
56 *
57 * Date:2010/05/09
58 * Contact:agilely@126.com
59 * Zju CS Lab
60 */
61
62
63 #include<stdio.h>
64  int main()
65 {
66 long n,num,cnt,res;
67 while(EOF != scanf("%d",&n))
68 {
69 cnt = 0;
70 while(n--)
71 {
72 scanf("%d",&num);
73 if(0 == cnt)
74 {
75 res = num;
76 cnt++;
77 }
78 else
79 {
80 if(res == num)
81 cnt++;
82 else
83 cnt--;
84 }
85 }
86 printf("%d\n",res);
87 }
88 return 0;
89 }
90

 

posted @ 2010-05-09 15:32  にんじゃ  阅读(1187)  评论(0)    收藏  举报