Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it
Chapter II computer problems
7-1 binary search (20 points)
1. Title Description
Enter n values (1 < = n < = 1000), n non descending integers and the number x to be found, use the binary search algorithm to find x, and output the subscript (0~n-1) where x is located and the number of comparisons. If X does not exist, output - 1 and the number of comparisons.
Input format:
There are three lines for input: the first line is the n value; The second line is n integers; The third line is the x value.
Output format:
The subscript (0~n-1) where x is output and the number of comparisons. If X does not exist, output - 1 and the number of comparisons.
Input example:
4 1 2 3 4 1 //No blank lines at the end
Output example:
0 2 //No blank lines at the end
2. Reference code
#include <iostream> using namespace std; int cnt=0; //#Binary search in the book: int BinarySearch(int a[],int x,int n) { int left=0; int right=n-1; while(left<=right) { int middle=(right+left)/2; if(x==a[middle]) { cnt++; return middle; } if(x>a[middle]) { cnt++; left=middle+1; } else{ cnt++; right=middle-1; } } return -1; } int main() { int n; cin>>n; int a[n]; for(int i=0;i<n;i++) { cin>>a[i]; } int x; cin>>x; cout<<BinarySearch(a,x,n)<<endl; cout<<cnt; }
The ordinary binary search method records the comparison times with cnt
7-2 rewrite binary search algorithm (20 points)
1. Title Description
Title Source: computer algorithm design and analysis, Wang Xiaodong
Let a[0:n-1] be an ordered array, rewrite the binary search algorithm so that when x is not in the array, the maximum element position i less than X and the minimum element position J greater than X are returned. When the search element is in the array, i and j are the same, both of which are the position of X in the array.
Input format:
The input has two lines:
The first line is n value and x value; The second line is a non descending sequence of n different integers, each separated by a space.
Output format:
The maximum subscript i of the largest element less than X and the minimum subscript j of the smallest element greater than X are output. When the search element is in the array, I and j are the same. Prompt: if x is less than all values, output: - 1 0. If x is greater than all values, output: the value of n-1 and the value of n
Input example:
A set of inputs is given here. For example:
6 5 2 4 6 8 10 12 //No blank lines at the end
Output example:
The corresponding output is given here. For example:
1 2 /// No blank lines at the end
2. Reference code
#include <iostream> using namespace std; void BinarySearch_2(int a[],int x,int left,int right,int &i,int &j) { while(left<=right) { int middle=(right+left)/2; if(x==a[middle]) { i=j=middle; return; } if(x>a[middle]) { left=middle+1; } else{ right=middle-1; } } i=right; j=left; return; } int main() { int n,x; cin>>n>>x; int a[n]; for(int i=0;i<n;i++) { cin>>a[i]; } int i,j; BinarySearch_2(a,x,0,n-1,i,j); cout<<i<<" "<<j; }
Use i and j to record the subscripts of left and right. i and j pass through the empty function BinarySearch_2 obtained after treatment
7-3 median of two ordered sequences (20 points)
1. Title Description
It is known that there are two equal length non descending sequences S1 and S2. Design the function to find the median of the union of S1 and S2. The median of ordered sequences A0,A1,..., a, n − 1 refers to the value of A(N − 1) / 2, that is, the ⌊ (N+1)/2 ⌋ (A0 is the first number).
Input format:
The input is divided into three lines. The first line gives the common length N of the sequence (0 < N ≤ 100000), and then input the information of a sequence in each line, that is, N integers arranged in non descending order. Numbers are separated by spaces.
Output format:
Outputs the median of the union sequence of two input sequences in one row.
Input example 1:
5 1 3 5 7 9 2 3 4 5 6 //No blank lines at the end
Output example 1:
4 //No blank lines at the end
Input example 2:
6 -100 -10 1 1 1 1 -50 0 2 3 4 5 //No blank lines at the end
Output example 2:
1
2. Reference code
#include <iostream> #include <algorithm> using namespace std; void Merge(int a[],int b[],int left,int mid,int right){ int i=left,j=mid+1,k=left; while((i<=mid)&&(j<=right)){ if(a[i]<=a[j]) { b[k]=a[i]; // cout<<b[k]<<"1"<<" "<<k<<endl; k++,i++; } else{ b[k]=a[j]; // cout<<b[k]<<"2"<<" "<<k<<endl; k++,j++; } } if(i>mid) { for(int q=j;q<=right;q++) { b[k++]=a[q]; } } else{ for(int q=i;q<=mid;q++){ b[k++]=a[q]; } } } void Copy(int a[],int b[],int left,int right) { for(int i=left;i<right;i++) { a[i]=b[i]; } } //void MergeSort(int a[],int left,int right) //{ // if(left<right) // { // int i=(left+right)/2; // MergeSort(a,left,i); // MergeSort(a,i+1,right); // Merge(a,b,left,i,right); // Copy(a,b,left,right); // // } //} int main() { int n; cin>>n; int a[2*n]; int b[2*n]; for(int i=0;i<2*n;i++) { cin>>a[i]; } // for(int i=0;i<2*n;i++) // { // cout<<a[i]<<" "; // } // cout<<endl; Merge(a,b,0,floor((2*n+1)/2)-1,2*n-1); // for(int i=0;i<2*n;i++) // { // cout<<b[i]<<" "; // } Copy(a,b,0,2*n-1); int res=floor((2*n+1)/2)-1; cout<<a[res]; }
This topic mainly uses the method of merging and sorting
Generally, output is not connected after cout < < endl
Introduction to the essence of C++endl