三个数可分等差数列
#include <iostream> #include <vector> using namespace std; void printGroups(vector<int>& sequence1, vector<int>& sequence2, vector<int>& sequence3, vector<int>& sequence4) { cout << "Group 1: "; for (int i = 0; i < sequence1.size(); i++) { cout << sequence1[i] << " "; } cout << endl; cout << "Group 2: "; for (int i = 0; i < sequence2.size(); i++) { cout << sequence2[i] << " "; } cout << endl; cout << "Group 3: "; for (int i = 0; i < sequence3.size(); i++) { cout << sequence3[i] << " "; } cout << endl; cout << "Group 4: "; for (int i = 0; i < sequence4.size(); i++) { cout << sequence4[i] << " "; } cout << endl; } void countWays(vector<int>& numbers, vector<int>& removed, vector<int>& sequence1, vector<int>& sequence2, vector<int>& sequence3, vector<int>& sequence4, int index, int& count) { if (sequence1.size() == 4 && sequence2.size() == 4 && sequence3.size() == 4 && sequence4.size() == 4) { count++; printGroups(sequence1, sequence2, sequence3, sequence4); cout << "Removed numbers: "; for (int i = 0; i < removed.size(); i++) { cout << removed[i] << " "; } cout << endl; cout << endl; return; } if (index >= numbers.size()) { return; } // 尝试将当前数添加到第一个等差数列中 if (sequence1.size() < 4) { if (sequence1.size() < 2 || numbers[index] - sequence1[sequence1.size() - 1] == sequence1[1] - sequence1[0]) { sequence1.push_back(numbers[index]); countWays(numbers, removed, sequence1, sequence2, sequence3, sequence4, index + 1, count); sequence1.pop_back(); } } // 尝试将当前数添加到第二个等差数列中 if (sequence2.size() < 4) { if (sequence2.size() < 2 || numbers[index] - sequence2[sequence2.size() - 1] == sequence2[1] - sequence2[0]) { sequence2.push_back(numbers[index]); countWays(numbers, removed, sequence1, sequence2, sequence3, sequence4, index + 1, count); sequence2.pop_back(); } } // 尝试将当前数添加到第三个等差数列中 if (sequence3.size() < 4) { if (sequence3.size() < 2 || numbers[index] - sequence3[sequence3.size() - 1] == sequence3[1] - sequence3[0]) { sequence3.push_back(numbers[index]); countWays(numbers, removed, sequence1, sequence2, sequence3, sequence4, index + 1, count); sequence3.pop_back(); } } // 尝试将当前数添加到第四个等差数列中 if (sequence4.size() < 4) { if (sequence4.size() < 2 || numbers[index] - sequence4[sequence4.size() - 1] == sequence4[1] - sequence4[0]) { sequence4.push_back(numbers[index]); countWays(numbers, removed, sequence1, sequence2, sequence3, sequence4, index + 1, count); sequence4.pop_back(); } } // 将当前数添加到已移除的数中 removed.push_back(numbers[index]); // 不将当前数添加到任何一个等差数列中 countWays(numbers, removed, sequence1, sequence2, sequence3, sequence4, index + 1, count); // 将当前数从已移除的数中移除 removed.pop_back(); } int main() { vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; vector<int> removed, sequence1, sequence2, sequence3, sequence4; int count = 0; countWays(numbers, removed, sequence1, sequence2, sequence3, sequence4, 0, count); cout << "Number of ways: " << count << endl; return 0; }