#include <bits/stdc++.h>
using namespace std;
int main()
{
char line[1000001];
while (cin.getline(line, 1000000))
{
int temp = 0, num = 0;
int preI = -INT_MAX, preJ = -INT_MAX;
vector<int> vec;
//input.
char *p = strtok(line, ",");
while (p)
{
sscanf(p, "%d", &temp);
vec.push_back(temp);
p = strtok(NULL, ",");
}
//com.
sort(vec.begin(), vec.end());
for (unsigned int i = 0; i < vec.size() - 1; i++)
{
if (vec[i] == preI)
{
continue;
}
preI = vec[i];
for (unsigned int j = i + 1; j < vec.size(); j++)
{
if (vec[j] == preJ)
{
continue;
}
preJ = vec[j];
for (int k = vec.size() - 1; k > j; k--)
{
if (vec[i] + vec[j] + vec[k] == 0)
{
num++;
break;
}
if (vec[i] + vec[j] + vec[k] < 0)
break;
}
}
}
// print.
cout << num;
}
return 0;
}
小米OJ #15 和为零的三元组
最新推荐文章于 2020-09-03 19:59:49 发布