算法问题,找假币(轻)C++实现(如有错误请指出)
//找假币
#include
using namespace std;
/*计算分组钱币总质量*/
int count(int a[],int begin,int end)
{
int sum = 0;
for (int i = begin; i <=end;i++)
{
sum += a[i];
}
return sum;
}
void find(int a[],int begin,int end)
{
int mid = (begin + end) / 2;
if(end-begin==1&&a[begin]>a[end]) //递归出口,end-begin代表只剩两个数(针对个数为偶数)
{
cout << "find no real money: " << end+1 << endl;
return ;
}
if(end-begin==1&&a[begin]<a[end]) //递归出口,end-begin代表只剩两个数(针对个数为偶数)
{
cout << "find no real money: " << begin+1 << endl;
return ;
}
if(begin-end==0)//递归出口,end-begin代表只剩一个数(针对个数为奇数)
{
cout << "find the jiabi" <<a[mid]<< endl;
return ;
}
if((begin+end)%2==1)//传入个数为偶数
{
if(count(a,begin,mid)<count(a,mid+1,end)) //(前面半组比后面半组轻)
{
find(a, begin, mid );
}
if(count(a,mid+1,end)<count(a,begin,mid))//前边半组比后面半组重
{
find(a, mid+1, end);
}
}
if((begin+end)%2==0&&count(a,begin,mid-1)==count(a,mid+1,end))//针对数组元素为个数时,两边数组相同则中间即为我们要找的假币
{
cout<<"Counterfeit money in the first"<<mid<<"individual"<<"The quality of"<<a[mid]<<endl;
return;
}
if((begin+end)%2==0)//针对数组个数为奇数
{
if(count(a,begin,mid-1)<count(a,mid+1,end))//前面比后面轻,递归前面
{
find(a, begin, mid - 1);
}
if(count(a,begin,mid-1)>count(a,mid+1,end))//前面比后面中,递归后面
{
find(a, mid + 1, end);
}
}
}
//测试
int main()
{
int a[9] = {2,2,2,2,2,2,2,2,1};
find(a, 0,sizeof(a)/sizeof(int)-1);
int a1[10] = {2,2,2,2,2,2,2,2,1,2};
find(a1, 0,sizeof(a1)/sizeof(int)-1);
int a2[9] = {2,2,2,2,1,2,2,2,2};
find(a2, 0,sizeof(a2)/sizeof(int)-1);
return 0;
}