深信服C/C++笔试A卷
1.给一个数组和一个数字k。前两个比大小,小的放最后面,大的算赢一次,赢k次,输出该大的数。(ac)
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> arr; int tmp = 0; while (cin >> tmp) { arr.push_back(tmp); } int k = arr.back(), len = arr.size() - 1; arr.resize(len); if (len <= k) { cout << *max_element(arr.begin(), arr.end()); return 0; } int count = 0; int maxval = arr[0]; for (int i = 1; count < k; i++) { if (maxval > arr[i]) { count++; arr.push_back(arr[i]); } else { count = 1; arr.push_back(maxval); maxval = arr[i]; } } cout << maxval; return 0; }
2.最大排序连续序列(ac90%)
#include <iostream> #include <vector> #include <algorithm> using namespace std; bool isright(vector<int> nums, int num, int i) { sort(nums.begin(), nums.end() - i); int j = 1; for (; j < num - i; j++) { if (nums[j] != nums[j - 1] + 1) break; } return j == num - i; } int main() { int num = 0; cin >> num; vector<int> nums(num); for (int i = 0; i < num; i++) cin >> nums[i]; /*int num = 5; vector<int> nums = { 3,1,2,4,6 };*/ for (int i = 0; i < num; i++) { if (isright(nums, num, i)) { cout << num - i; return 0; } } cout << 1; return 0; }
3.病毒侵入(ac,转化为区间即可)
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n = 0, x = 0; cin >> n >> x; vector<int> nums(n); for (int i = 0; i < n; i++) cin >> nums[i]; /*int n = 5,x = 4; vector<int> nums = { 4,9,6,7,8 };*/ vector<vector<int>> arr; for (int i = 0; i < n; i++) arr.push_back({ nums[i] - x, nums[i] + x }); vector<int> jiaoji = arr[0]; int count = 0; for (int i = 1; i < n; i++) { if (jiaoji[0] > arr[i][1] || jiaoji[1] < arr[i][0]) { count++; jiaoji = arr[i]; } else {//有交集 jiaoji = { max(arr[i][0], jiaoji[0]), min(arr[i][1], jiaoji[1]) }; } } cout << count; return 0; }
#深信服笔试题##深信服##深信服校招#