Sherlock and Squares

Source: Internet
Author: User

Topic:

Watson gave Sherlock two integers a and _b_, and now Watson asks Sherlock if he can calculate the number of complete squares between A and _b_ (including A and B). The total square number refers to the square of any integer. For example,1, 4, 9, 16 is the complete square number, because they are 1, 2,3 1≤t≤100 1≤a≤b≤109 input Sample 917 Output Sample  In a set of test data, 4 and 9 are full squares. In the second set of test data, between 17 and 24 (inclusive of 17 and 24), there is no complete square. 

Analysis: The main application of the floor () and ceil () two functions in math, the Java code is as follows:

     Public Static intSquaresnums (intAintb) {return(int) (Math.floor (math.sqrt (b))-Math.ceil (Math.sqrt (a))) +1; }     Public Static voidMain (string[] args) {/*Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scanner in=NewScanner (system.in); intt =In.nextint ();  for(inta0 = 0; A0 < T; a0++){            intA =In.nextint (); intb =In.nextint ();        System.out.println (Squaresnums (A, b)); }    }

Sherlock and Squares

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.