#include <iostream> #include <vector> #include <algorithm> int main() { int n; std::cin >> n; std::vector<int> nums(n); for (int i = 0; i < n; i++) { std::cin >> nums[i]; } int count = 0; for (int i = 1; i <= n; i++) { nums.push_back((i * (i + 1)) / 2); } std::sort(nums.begin(), nums.end()); for (int i = 0; i < nums.size(); i++) { std::cout << nums[i] << " "; } std::cout << std::endl; for (int i = 0; i < n - 2; i++) { int a = nums[i]; int start = i + 1; int end = n - 1; while (start < end) { int b = nums[start]; int c = nums[end]; if (a + b + c == 0) { std::cout << a << " " << b << " " << c << std::endl; count++; start = start + 1; end = end - 1; } else if (a + b + c > 0) { end = end - 1; } else { start = start + 1; } } } std::cout << count << std::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 51 52 53 54 55 56 | #include <iostream> #include <vector> #include <algorithm> int main() { int n; std::cin >> n; std::vector<int> nums(n); for (int i = 0; i < n; i++) { std::cin >> nums[i]; } int count = 0; for (int i = 1; i <= n; i++) { nums.push_back((i * (i + 1)) / 2); } std::sort(nums.begin(), nums.end()); for (int i = 0; i < nums.size(); i++) { std::cout << nums[i] << " "; } std::cout << std::endl; for (int i = 0; i < n - 2; i++) { int a = nums[i]; int start = i + 1; int end = n - 1; while (start < end) { int b = nums[start]; int c = nums[end]; if (a + b + c == 0) { std::cout << a << " " << b << " " << c << std::endl; count++; start = start + 1; end = end - 1; } else if (a + b + c > 0) { end = end - 1; } else { start = start + 1; } } } std::cout << count << std::endl; return 0; } |