标签:个数 输出 class printf bsp include clu ++ for
一个数组中出现次数超过一半的元素为主元素。找出主元素,如果不存在输出None
代码:
#include <stdio.h> #define MAX 100 int main() { int n,t = -1,c = 0; int d[MAX]; scanf("%d",&n); for(int i = 0;i < n;i ++) { scanf("%d",&d[i]); if(d[i] == t) c ++; else c --; if(c < 0) t = d[i],c = 1; } c = 0; for(int i = 0;i < n;i ++) { if(t == d[i]) c ++; } if(c * 2 > n) printf("%d",t); else printf("None"); return 0; }
标签:个数 输出 class printf bsp include clu ++ for
原文地址:https://www.cnblogs.com/8023spz/p/11870127.html