A square number is an integer number whose square root is also an integer. For example 1, 4, 81 are
some square numbers. Given two numbers a and b you will have to find out how many square numbers are there between a and b (inclusive).
Input
The input file contains at most 201 lines of inputs. Each line contains two integers a and b (0 < a ≤
b ≤ 100000). Input is terminated by a line containing two zeroes. This line should not be processed.
Output
For each line of input produce one line of output. This line contains an integer which denotes how
many square numbers are there between a and b (inclusive).
Sample Input
1 4
1 10
0 0
Sample Output
2
3
大意:給兩個數字 求範圍內平方不大於第二個數字的數量
解法: 以最大數取根號後往回看
import java.util.Scanner;
public class UVA11461 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int start = sc.nextInt();
int last = sc.nextInt();
if (last == 0)
break;
System.out.println((int) Math.sqrt(last)
- (int) Math.ceil(Math.sqrt(start)) + 1);
}
sc.close();
}
}
some square numbers. Given two numbers a and b you will have to find out how many square numbers are there between a and b (inclusive).
Input
The input file contains at most 201 lines of inputs. Each line contains two integers a and b (0 < a ≤
b ≤ 100000). Input is terminated by a line containing two zeroes. This line should not be processed.
Output
For each line of input produce one line of output. This line contains an integer which denotes how
many square numbers are there between a and b (inclusive).
Sample Input
1 4
1 10
0 0
Sample Output
2
3
大意:給兩個數字 求範圍內平方不大於第二個數字的數量
解法: 以最大數取根號後往回看
import java.util.Scanner;
public class UVA11461 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int start = sc.nextInt();
int last = sc.nextInt();
if (last == 0)
break;
System.out.println((int) Math.sqrt(last)
- (int) Math.ceil(Math.sqrt(start)) + 1);
}
sc.close();
}
}
留言
張貼留言