这貌似是今年阿里JAVA在线编程测验题目,我把它转为C++做出来了,欢迎大家提出建议。
#include <iostream>
#include<stdbool.h>
using namespace std;
bool CheckMiddle(int start, int end, int sum, int a[]);
//从左边右边各自开始求和,当结果相等时判断!
bool resolve(int a[],int num)
{
bool result = false;
int i = 0;
int j = num-1;
int left = a[i];
int right = a[j];
while (i < j)
{
if (left < right)
{
i++;
left += a[i];
}
else if(left>right)
{
j--;
right += a[j];
}
else if(left == right)
{
result = CheckMiddle(i+1,j-1,left,a);
if (result)
return result;
else
{
i++;
left += a[i];
}
}
}
return result;
}
//判断中间区域
bool CheckMiddle(int start,int end,int sum,int a[])
{
bool result = false;
int i = start + 1;
int j = end - 1;
int left = a[i];
int right = a[j];
while (i<j)
{
if (left < right)
{
i++;
left += a[i];
}
else if (left > right)
{
j--;
right += a[j];
}
else if (left == right)
{
if ((j - i == 2) && left == sum)
{
return true;
}
else
return false;
}
}
return false;
}
//主函数进行判断
int main()
{
//int A[11] = {2,5,1,1,1,1,4,1,7,3,7};
int A[9] = {10,2,11,13,1,1,1,1,1};
bool Result = resolve(A,sizeof(A)/sizeof(A[0]));
if (Result == 1)
cout << "true" << endl;
else
cout << "false" << endl;
}