给定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;
}