An array a of n integer elements, find the elements that appear more than n / 2

https://www.cnblogs.com/gczr/p/8334459.html

Consider this example:

5 1 5 4 1 1 3 1 2 1 1

There are 11 numbers in total, of which 1 appears 6 times. So how to find this 6 quickly? Let’s consider an example in real life: there is a group of people fighting in groups, and each of them has a number to represent their own power. Now this group of people walks towards the center of the square in order. If there is a group of people with themselves in the square now If he is not a member of the same power, then he will die with one of them, and ask, which power will win in the end? Let's look at the example just now according to this meaning:

1) A person of faction 5 walks into the center of the square, now there is a person in the center of the square, and the influence is 5

2) A person of faction 1 walked into the center of the square, and he found a person of faction 5 in the middle of the square, so they died together. Now there is no one on the square

3) A person of faction 5 walks into the center of the square, now there is a person in the center of the square, and the influence is 5

4) A person of faction 4 walked into the center of the square, and he found a person of faction 5 in the middle of the square, so he died together. Now there is no one on the square

5) A person of faction 1 walks into the center of the square, now there is a person in the square, and the faction is 1.

6) One person from faction 1 walks into the center of the square, now there are two people in the square, and the faction is 2.

……

It can be found that in each fight, the one with the greatest power (that is, the number with the most occurrences) will only die each time. Rushing will be carried out at most N/2 times, and the number with the highest frequency will only lose N/2 at most, and the title tells us that the number of appearances has exceeded half, so the number of the remaining gang on the square must be there. The one with more reps!

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

int findPossibleMain(int A[], int n) {//找出最大势力
	int x = 1;//开始广场上有一个人
	int main = A[0];//这个人的势力是A[0]
	for (int i = 1; i < n; i++) {//每一轮各个势力派一人上广场PK
		if (x == 0)
		{ //广场没人,此势力占领广场,人数为1
			main = A[i];
			x = 1;
		}
		else
		{
			if (main==A[i])
			{
				x++;//广场上的势力和进场势力同属一个,人数加1
			}
			else //火拼的次数最多为n/2,每次火拼主势力最多损失1人(损失0人是此场火拼主势力没参与)
			{
				x--;//不同势力,火拼后同归于尽
			}
		}
	}
	return main;
}
int main() {
	//int A[maxn]={0,5,5,3,5,7,5,5};
	//int A[maxn] = { 0,5,5,3,5,1,5,7 };
	int n,A[100];
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &A[i]);
	}
	int main = findPossibleMain(A, n);//可能的主元素
	printf("%d\n", main);
	int x = 0;//可能的主元素出现的次数
	for (int i = 0; i < n; i++)
	{
		if (A[i] == main)
		{
			x++;
		}
	}	
	if (x > n / 2)
	{
		printf("所求的元素:%d" , main);
	}
	else
	{
		printf("没有这样的元素");
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_43496435/article/details/113804143