求主元素

问题描述:有一个整数数列,数列中元素出现次数超过一半的元素定义为主元素,设计一分治算法,求出主元素。
输入:整数的个数n以及n个整数

输出:如果有主元素,输出主元素以及它们所在的位置;如果没有主元素,输出-1.

考研数据结构题:

#include <iostream>
using namespace std;
int searchMain(int A[],int n)
{
	int i;
	int c=A[0],count=1;
	for(i=1;i<n;i++)
	{
		if(A[i]==c)
		{

			count++;
		}else{
			if(count>0)
			{
				count--;
			}else{
				c=A[i];
				count=1;
			}

		}
	}
	if(count>0)
	{

		for(i=count=0;i<n;i++)
		{

			if(A[i]==c)
			{

				count++;
			}
		}
	}

	if(count>n/2)
	{

		return c;
	}else{
		return -1;
	}

}

#define LEN(arr) sizeof(arr)/sizeof(arr[0])

int main(int argc,char* argv[])
{
	int count;    
	cin>>count;  
	while (count--) {  
		int n;
		cin>>n;
		//int a[]={0,5,5,7,5,5,3,6,5,3,3,3,3,3,3,3,3,3,3,8};
		int *a=new int[n];//输入动态数组
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
		}		
		int c=searchMain(a,n);
		printf("%d\n",c);

	}	
	return 0;
}
#include <iostream>
using namespace std;
int searchMain(int A[],int n)
{
	int i;
	int c=A[0],count=1;
	for(i=1;i<n;i++)
	{
		if(A[i]==c)
		{

			count++;
		}else{
			if(count>0)
			{
				count--;
			}else{
				c=A[i];
				count=1;
			}

		}
	}
	if(count>0)
	{

		for(i=count=0;i<n;i++)
		{

			if(A[i]==c)
			{

				count++;
			}
		}
	}

	if(count>n/2)
	{

		return c;
	}else{
		return -1;
	}
}
//计数排序
int Major(int A[],int n)
{
	int i,*p,max;
	p=(int*)malloc(sizeof(int)*n);
	for(i=0;i<n;i++)
	{
		p[i]=0;
	}
	max=0;
	for(i=0;i<n;i++)
	{

		p[A[i]]++;
		if(p[A[i]]>p[max])
		{

			max=A[i];
		}
	}
	if(p[max]>n/2)
	{

		return max;
	}else{
		return -1;
	}

}


#define LEN(arr) sizeof(arr)/sizeof(arr[0])

int main(int argc,char* argv[])
{
	int count;    
	cin>>count;  
	while (count--) {  
		int n;
		cin>>n;
		//int a[]={0,5,5,7,5,5,3,6,5,3,3,3,3,3,3,3,3,3,3,8};
		int *a=new int[n];//输入动态数组
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
		}		
		int c=Major(a,n);
		printf("%d\n",c);

	}	
	return 0;
}



好的,以下是分治算法求主元素的Python代码实现: ```python def majority_element(nums): """ 分治算法求主元素 :param nums: 数组 :return: 主元素 """ def majority_element_rec(lo, hi): # 当只有一个元素时,该元素即为主元素 if lo == hi: return nums[lo] # 分治:分别左半部分和右半部分的主元素 mid = (hi - lo) // 2 + lo left_majority = majority_element_rec(lo, mid) right_majority = majority_element_rec(mid + 1, hi) # 如果左右半部分的主元素相同,则该元素即为整个数组的主元素 if left_majority == right_majority: return left_majority # 如果左右半部分的主元素不同,则需要统计两个主元素在整个数组中出现的次数,从而确定整个数组的主元素 left_count = sum(1 for i in range(lo, hi + 1) if nums[i] == left_majority) right_count = sum(1 for i in range(lo, hi + 1) if nums[i] == right_majority) return left_majority if left_count > right_count else right_majority return majority_element_rec(0, len(nums) - 1) ``` 其中,`majority_element_rec`是递归函数,用于解子数组的主元素。具体地,该函数首先递归地解左半部分和右半部分的主元素,然后再根据左右半部分的主元素是否相同,分别统计左右半部分主元素在整个数组中出现的次数,从而确定整个数组的主元素。最后,`majority_element`函数调用`majority_element_rec`函数,并将数组的起始和终止位置作为参数传入。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值