#include <iostream> #include <vector> #include <algorithm> using namespace std; int threeSum(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> build_buc() { int n; cin >> n; vector<int> like_array(n); for (int i = 0; i < n; ++i) { cin >> like_array[i]; } vector<int> res; for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { int sum = 0; for (int k = i; k <= j; ++k) { sum += like_array[k]; } res.push_back(sum); } } return res; } int main() { vector<int> buc = build_buc(); if (buc.size() < 3) { cout << 0 << endl; } else { cout << threeSum(buc) << endl; } 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 42 43 44 45 46 47 48 49 50 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int threeSum(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> build_buc() { int n; cin >> n; vector<int> like_array(n); for (int i = 0; i < n; ++i) { cin >> like_array[i]; } vector<int> res; for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { int sum = 0; for (int k = i; k <= j; ++k) { sum += like_array[k]; } res.push_back(sum); } } return res; } int main() { vector<int> buc = build_buc(); if (buc.size() < 3) { cout << 0 << endl; } else { cout << threeSum(buc) << endl; } return 0; } |