QQ扫一扫联系
(寻找被移除的元素)问题:原有长度为n+1公差为1等升数列,将数列输到程序的数组时移除了一个元素,导致长度为n的开序数组可能不再连续,除非被移除的是第一个或最后一个元素。需要在数组不连续时,找出被移除的元素。试补全程序。
#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 + __(1)__ ) { __(2)__ ; } else { __(3)__ ; } } return __(4)__ ; } 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 == __(5)__) { cout << "Sequence is consecutive" << endl; } else { cout << "Missing number is " << missing_number << endl; } return 0; }
①处应填( )
1
nums[0]
right
left
②处应填( )
left=mid+1
right=mid-1
right=mid
left=mid
③处应填( )
left=mid+1
right=mid-1
right=mid
left=mid
④处应填( )
left+nums[0]
right+nums[0]
mid+nums[0]
right+1
⑤处应填( )
nums[0]+n
nums[0]+n-1
nums[0]+n+1
nums[n-1]