1001

来源:互联网 发布:java version是什么 编辑:程序博客网 时间:2024/04/20 21:18
 

寻找相同

Time Limit:1000MS  Memory Limit:1000K
Total Submit:206 Accepted:49

Description

有一组数,很多很多个数,里面有一个数出现了超过一半次,请你把它找出来。

Input

先是一个N (N<=400000),然后接下来一行N个数,多组数据输入。

Output

对每个Case,输出一行,这一行只含有一个在之前数列中出现超过一半次的数。每个case之后输出一个空行。

Sample Input

11 5 5 5 5 5 5 1 2 3 4 6

Sample Output

Case 1: 5
 
代码如下:
#include<stdio.h>
#include<iostream>
using namespace std;
int stack[200001];
int main()
{
    int i,j=0,m,t,se,ss;
    while(scanf("%d",&m)!=EOF)
    {
        se=ss=0;                     
        scanf("%d",&t);                     
        stack[se++]=t;
        for(i=1;i<m;i++)
        {
            scanf("%d",&t);
            if(t!=stack[se-1])  se--;
            else stack[se++]=t;
            if(se==0)
            {
                i++;     
                scanf("%d",&stack[se++]);                     
            }
        }
        printf("Case %d: %d/n",++j,stack[ss]);
        printf("/n");
    }
    system("pause");
    return 0;
}