数组四等分问题
bool func(vector<int>& vec) {
int m1 = 0;
int m2 = 1;
int m3 = 2;
int sumA = 0;
int sumB = 0;
int sumC = 0;
int sumD = 0;
int length = (int)vec.size();
for (int i = 3; i < length; i++)
sumD += vec[i];
while (true) {
if(sumC < sumD) {
sumC += vec[m3];
sumD -= vec[m3 + 1];
++m3;
if(m3 == length)
break;
else
continue;
}
if(sumB < sumC) {
sumB += vec[m2];
sumC -= vec[m2 + 1];
++m2;
if(m2 == m3)
break;
else
continue;
}
if(sumA < sumB) {
sumA += vec[m1];
sumB -= vec[m1 + 1];
++m1;
if(m1 == m2)
break;
else
continue;
}
if(sumA == sumB &&
sumB == sumC &&
sumC == sumD)
return true;
}
return false;
}
网友评论