hdu 1029

来源:互联网 发布:化工过程模拟软件 编辑:程序博客网 时间:2024/05/19 00:52

题目描述:

                 输入一组数,n个,将该组数中相同数字的个数大于(n+1)/ 2 的数字输出。

题目类型:

                 简单哈希。

源代码:

解法一:

#include<stdio.h>#include<string.h>int main(){int N;int a[50000];int temp;int i,j;while(scanf("%d",&N)!=EOF){memset(a,0,sizeof(a));for(i=1;i<=N;i++){scanf("%d",&temp);a[temp]++;if(a[temp]>=(N+1)/2)j=temp;    //关键}printf("%d\n",j);            //注意输出的是j,不是 temp}return 0;}


解法二:

             来自于网上,但感觉方法挺巧妙的。

源代码:

#include <stdio.h>   #include <stdlib.h>   int main()    {        int ans, num, count, n;        while (scanf("%d", &n) != EOF)        {            count = 0;           for (int i=1; i<=n; i++)           {               scanf("%d", &ans);               if (count == 0)               {                   num = ans;                   count ++;               }               else                  if (ans == num)                      count++;                 else count--;           }           printf("%d\n", num);        }     return 0;     }