#include <bits/stdc++.h> using namespace std; int countZeroTriplets(vector<int>& nums) { int count = 0; int n = nums.size(); for (int i = 0; i < n - 2; ++i) { for (int j = i + 1; j < n - 1; ++j) { for (int k = j + 1; k < n; ++k) { if (nums[i] + nums[j] + nums[k] == 0) { ++count; } } } } return count; } vector<int> sums; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; ++i) { cin >> arr[i]; } for (int i = 0; i < n; ++i) { int sum = 0; for (int j = i; j < n; ++j) { sum += arr[j]; sums.push_back(sum); } } cout<<countZeroTriplets(sums); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <bits/stdc++.h> using namespace std; int countZeroTriplets(vector<int>& nums) { int count = 0; int n = nums.size(); for (int i = 0; i < n - 2; ++i) { for (int j = i + 1; j < n - 1; ++j) { for (int k = j + 1; k < n; ++k) { if (nums[i] + nums[j] + nums[k] == 0) { ++count; } } } } return count; } vector<int> sums; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; ++i) { cin >> arr[i]; } for (int i = 0; i < n; ++i) { int sum = 0; for (int j = i; j < n; ++j) { sum += arr[j]; sums.push_back(sum); } } cout<<countZeroTriplets(sums); return 0; } |