(寻找被移除的元素)问题:原有长度为 n+1公差为1等升数列,将数列输到程序的数组时移除了一个元素,导致长度为 n 的开序数组可能不再连续,除非被移除的是第一个或最后之个元素。需要在数组不连续时,找出被移除的元素。
试补全程序。
01 #include <iostream 02 #include <vector> 03 04 using namespace std; 05 06 int find missing(vector<int>& nums) ( 07 int left = 0, right = nums.size() - 1; 08 while (left < right){ 09 int mid = left + (right left) / 2; 10 if (nums[mid] - mid+ ①) ( 11 ②; 12 }else{ 13 ③ 14 } 15 } 16 return ④; 17 } 18 19 int main() ( 20 int n; 21 cin >> n; 22 vector<int> nums(n); 23 for (int i= 0; i< n; i++) cin >> nums[i]; 24 int missing_number = find_missing(nums); 25 if_(missing_number == ⑤) { 26 cout << "Sequence is consecutive" << endl; 27 }else{ 28 cout << "Missing number is " << ,missing numbeer << endl; 29 } 30 return 0; 31 }
①处应填( )
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]