当前位置:编程学习 > C/C++ >>

HDU 1029 Ignatius and the Princess IV

给N个数字, N为奇数, 输出出现次数大于 N / 2 的数  1 #include <stdio.h>
 2 #include <stdlib.h>
 3
 4 int main()
 5 {
 6     int ans, num, count, n;
 7     while (scanf("%d", &n) != EOF)
 8     {
 9         count = 0;
10         for (int i=1; i<=n; i++)
11         {
12             scanf("%d", &ans);
13             if (count == 0)
14             {
15                 num = ans;
16                 count ++;
17             }
18             else if (ans == num)count++;
19             else count--;
20         }
21         printf("%d\n", num);
22     }
23 }
24

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,