Nefu 899 It's also a naked look.

Source: Internet
Author: User

#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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.