英雄会上的一些题

来源:互联网 发布:mysql select 查询 表 编辑:程序博客网 时间:2024/04/26 00:47

题2:完全平方数的个数

题目详情

给定整数区间[A,B]问其中有多少个完全平方数。

输入格式:

多组数据,包含两个正整数A,B 1<=A<=B<=2000000000。

输出格式:

每组数据输出一行包含一个整数,表示闭区间[A,B]中包含的完全平方数的个数。

答题说明

输入样例

1 1

1 2

3 10

3 3

输出样例:

1

1

2

0

我提交的代码
import java.util.Scanner;public class PerfectIntegerCounter {    public static void main(String[] args) {        Scanner inputer = new Scanner(System.in);        while(inputer.hasNext())        {              int low = inputer.nextInt();            int high = inputer.nextInt();            System.out.println(reckon(low, high));        }        inputer.close();    }        private static int reckon(int low, int high){        low = (int)Math.ceil(Math.sqrt(1.0*low));        high = (int)Math.floor(Math.sqrt(1.0*high));        return (high-low+1);    }}


0 0
原创粉丝点击