#include <iostream>#include<algorithm>#include<cstdio>using namespacestd;intMain () {intn,k; while(SCANF ("%d%d", &n,&k)!=-1) { intR=1, l=n,mid,sum=0; while(r<=l) {sum++; Mid= (r+l)/2; if(mid==k) {printf ("%d\n", sum); Break;} if(mid<k) r=mid+1; Elsel=mid-1; } } return 0;}
We now know that the subject of the binary lookup is a while loop, which is queried within each loop to determine whether the data stops looping or changes the range of the search interval. Now is the time to test whether you understand the principle! How many times does it take to find K in the sequence {1,2,3,......,n} using two-point lookup?
Input
Enter more than one set of data, each set of two integers n and K, one row (0<= k <= N).
Output
For each set of inputs, the output finds the number of cycles of k in the sequence {1,2,3,......,n}.
Sample Input
5 25 110 3
Sample Output
323
Nefu 899 It's also a naked look.