题目链接 https://code.mi.com/problem/list/view?id=20
代码
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 int cnt = 0; 6 7 void getNumVec(vector<int>& v, string s) 8 { 9 int temp = 0, flag = 1; 10 for (int i = 0;i < s.length(); ++i) 11 { 12 if (s[i] == ' ') 13 { 14 v.push_back(temp * flag); 15 temp = 0; 16 flag = 1; 17 } 18 else if (s[i] == '-') 19 { 20 flag = -1; 21 } 22 else 23 { 24 temp *= 10; 25 temp += s[i] - '0'; 26 } 27 } 28 v.push_back(temp * flag); 29 } 30 31 bool check(vector<int> v) 32 { 33 int sz = v.size(); 34 if (sz < 2) 35 { 36 return false; 37 } 38 39 if (sz == 2) 40 { 41 return true; 42 } 43 44 int d = v[1] - v[0]; 45 for (int i = 2; i < sz; ++i) 46 { 47 if (v[i] - v[i - 1] != d) 48 { 49 return false; 50 } 51 } 52 return true; 53 } 54 55 void dfs(vector<int>& v, vector<int>& temp, int start, int sz) 56 { 57 bool flag = false; 58 for (int i = start;i < sz; ++i) 59 { 60 temp.push_back(v[i]); 61 flag = check(temp); 62 if (flag || temp.size() < 2) //剪枝 63 { 64 if (flag) 65 { 66 ++cnt; 67 } 68 dfs(v, temp, i + 1, sz); 69 } 70 temp.pop_back(); 71 } 72 } 73 74 int main() 75 { 76 string s; 77 getline(cin, s); 78 79 vector<int> v, temp; 80 getNumVec(v, s); 81 int sz = v.size(); 82 dfs(v, temp, 0, sz); 83 84 cout << cnt << endl; 85 86 return 0; 87 }