2 Star 9 Fork 5

xusun000 / 408-所有真题算法

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
2013_code_mian_ele.cpp 690 Bytes
一键复制 编辑 原始数据 按行查看 历史
xusun000 提交于 2021-12-01 16:31 . 更改名称
#include<stdio.h>
#include<stdlib.h>
int majority(int A[], int length) {
int p = 0;
int start = 0, count = 0;
for (int j = 0;j < length;j++) {
if (A[start] == A[j]) count++;
else count--;
if (count == 0 && j < length - 1) {
start = ++j;
count = 1;
}
}
int mainElement = A[start];
int countElement = 0;
for (int j = 0;j < length;j++) {
if (A[j] == mainElement)countElement++;
}
return countElement > length / 2 ? 1 : 0;
}
int main() {
int A[] = { 1,1,0,0,0,1,1,1,0 };
int isMajority = majority(A, 9);
if (isMajority)printf("有主元素");
else printf("没有主元素");
}
1
https://gitee.com/xusun000/exam_code_for_408.git
git@gitee.com:xusun000/exam_code_for_408.git
xusun000
exam_code_for_408
408-所有真题算法
master

搜索帮助