深信服9.1笔试
题比较简单,直接上代码
1.准备一个额外空间,,存放比较胜者,直接比较过去,记录比较次数
nt main1() { vector<int>arr; int t = 0; while (cin>>t) { arr.push_back(t); if (cin.get() == '\n') { break; } } int k = arr[arr.size() - 1]; arr.pop_back(); int left = 1; int comnum = arr[0]; int cnt = 0; while (left < arr.size()) { if (comnum > arr[left]) { cnt++; } else { cnt = 1; comnum = arr[left]; } left++; if (cnt == k) { break; } } string s = to_string(comnum); cout << s; system("pause"); return 0; }2.最长连续序列********128
int lonest(vector<int>& v) { int res = 0; unordered_set<int>rec(v.begin(), v.end()); for (auto n : rec) { if (rec.count(n - 1)) { continue; } int cnt = 1; while (rec.count(++n)) { ++cnt; } res = max(res, cnt); } return res; } int main2() { int n; cin >> n; vector<int>arr; while (n--) { int a; cin >> a; arr.push_back(a); } if (arr.size() == 1) { return arr.size(); } cout << lonest(arr) << endl; system("pause"); return 0; }3.第三题遍历数组,维护最大值与最小值,当最大值与最小值的差大于2*x,伪装次数加1,最大最小值变为当前遍历值
int main() { int n, x; cin >> n >> x; vector<int>arr; while (n--) { int a; cin >> a; arr.push_back(a); } int res = 0; int maxarr = arr[0]; int minarr = arr[0]; for (int i = 0; i < arr.size(); i++) { maxarr = max(maxarr, arr[i]); minarr = min(minarr, arr[i]); if (maxarr - minarr > 2 * x) { res++; maxarr = arr[i]; minarr = arr[i]; } } cout << res << endl; system("pause"); return 0; }