每日一题算法实现

给定n个整数的数组A以及一个数x,设计一个分治算法,求出x在数组中出现的次数,

//查找数组中指定数字出现的个数
#include<stdio.h>
//把数组分开,当数组还剩一个的时候可以判断是否是想要的数字,并且计数
void fuction(int *L,int x,int low,int high,int high1);
int Partition(int *L,int low,int high);

int main()
{
	int count;
	int a[12]={21,32,34,21,32,21,4,34,54,21,21,0};//多用一个位置记录次数
	int low=0,high=10,high1=11;
	fuction(a,21,low,high,high1);
	printf("你想要查找的数字%d 出现的次数为%d\n",21,a[high1]);
	return 0;
}
void fuction(int *L,int x,int low,int high,int high1){
	int pivotloc=0;
	if(low<high){
		pivotloc=Partition(L,low,high);
		printf("low=%d high=%d\n",low,high);
		fuction(L,x,low,pivotloc,high1);
		fuction(L,x,pivotloc+1,high,high1);
	}
	else{
		if(L[low]==x){
			L[high1]+=1; 
			//printf("你想要查找的数字%d 出现的次数为%d\n",x,L[high1]);
			}
	}
}
int Partition(int *L,int low,int high)//返回数组的中间位置
{
	return (high+low)/2;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值