(寻找被移除的元素)问题: 原有长度为 n+1n+1 公差为 11 等差数列,将数列输到程序的数组时移除了一个元素,导致长度为 nn 的连续数组可能不再连续,除非被移除的是第一个或最后一个元素。需要在数组不连续时,找出被移除的元素。试补全程序。
1①处应填( )
- #include <iostream>
- #include <vector>
- using namespace std;
- int find_missing(vector<int>& nums) {
- int left = 0, right = nums.size() - 1;
- while (left < right){
- int mid = left + (right - left) / 2;
- if (nums[mid] == mid + ①) {
- ②;
- } else {
- ③;
- }
- }
- return ④;
- }
- int main() {
- int n;
- cin >> n;
- vector<int> nums(n);
- for (int i = 0; i < n; i++) cin >> nums[i];
- int missing_number = find_missing(nums);
- if (missing_number == ⑤) {
- cout << "Sequence is consecutive" << endl;
- }else{
- cout << "Missing number is " << missing_number << endl;
- }
- return 0;
- }
2②处应填( )
3③处应填( )
4④处应填( )
5⑤处应填( )
1.A. 1
B. nums[0]
C. right
D. left
2.
A. left=mid+1
B. right=mid-1
C. right=mid
D. left=mid
3.
A. left=mid+1
B. right=mid-1
C. right=mid
D. left=mid
4.
A. left+nums[0]
B. right+nums[0]
C. mid+nums[0]
D. right+1
5.
A. nums[0]+n
B. nums[0]+n-1
C. nums[0]+n+1
D. nums[n-1]