把一个数组分成三份,两个分割点不算进求和中,使得每份的和要相同
程序员文章站
2022-03-10 12:08:48
...
#include <iostream>
#include <vector>
using namespace std;
int position(vector<int>sum, int i, int j, int len)
{
int flag = 0;
while (i < j)
{
if (i<j&&j<len)
{
if (sum[len - 1] - sum[j] == sum[i] && sum[i] == sum[j - 1] - sum[i+1])
{
flag = 1;
cout << i+1 << " " << j << endl;
break;
}
}
--j;
}
return flag;
}
int main()
{
//int a[] = {2,5,1,3,4,6,7};
int n;
while (cin >> n)
{
vector<int>a;
for (int i = 0; i < n; ++i)
{
int tmp;
cin >> tmp;
a.push_back(tmp);
}
//---------分割线-------
vector<int>sum;
int s = 0;
for (size_t i = 0; i < a.size(); ++i)
{
s += a[i];
sum.push_back(s);
}
int len = sum.size();
size_t i = 1, j = len - 2;
for (; i < len; ++i)
{
int flag = position(sum, i, j, len);
if (flag == 1)
break;
}
}
return 0;
}
上一篇: redis发布订阅
下一篇: C++ rapidjson 基础入门