mirror of
https://github.com/xusun0623/exam_code_for_408.git
synced 2026-02-02 18:09:04 +08:00
add 2013
This commit is contained in:
28
code_mian_ele_2013.cpp
Normal file
28
code_mian_ele_2013.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#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("没有主元素");
|
||||
}
|
||||
Reference in New Issue
Block a user