利用分治法解决假硬币问题(C++)

#include
using namespace std;
int sum(int a[],int left,int right)
{
    int sum=0;
    for(int i=left;i<=right;i++)  sum+=a[i];
    return sum;
}
int FindCorn(int a[], int left, int right)
{
    int sum1,sum2;
    sum1=sum2=0;
    if(left+1==right)   //偶数个硬币只剩下两枚,无法进行分组,可直接进行比较
	{
        if(a[left]sum2) return FindCorn(a, left+(right-left)/2+1, right);          
//如果左组重量总和偏低,则假硬币在左组,反之亦然
        else if(sum1sum2) return FindCorn(a ,left+(right-left)/2+1, right);
        if(sum1> n;
	cout << "请一个一个输入这些硬币的重量" << endl;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	cout << "假币是第" << FindCorn(a,0,n-1) << "个硬币";
    return 0;
}

你可能感兴趣的:(c++,蓝桥杯,算法)