输入两个正整数A, B,求[A, B]内完全平方数的个数。( 1 ≤ A ≤ B ≤ 10^9 ) 测试时输入有极大的数,如:385793959 712365911 ,因此要用long类型存储。
时间: 2024-06-14 09:04:09 浏览: 39
以下是一个求解[A, B]内完全平方数个数的示例代码:
```python
import math
def count_square_numbers(A, B):
count = 0
start = math.ceil(math.sqrt(A))
end = math.floor(math.sqrt(B))
count = end - start + 1
return count
A = int(input("请输入A的值:"))
B = int(input("请输入B的值:"))
result = count_square_numbers(A, B)
print("在区间[{0}, {1}]内的完全平方数个数为:{2}".format(A, B, result))
```
相关问题
输入两个正整数A, B,求[A, B]内完全平方数的个数。( 1 ≤ A ≤ B ≤ 10^9 )
以下是一个求解[A, B]内完全平方数个数的Python程序示例:
```python
import math
def count_perfect_squares(A, B):
count = 0
for i in range(A, B+1):
if math.isqrt(i)**2 == i:
count += 1
return count
A = int(input("请输入A的值:"))
B = int(input("请输入B的值:"))
result = count_perfect_squares(A, B)
print("在[%d, %d]内的完全平方数个数为:%d" % (A, B, result))
```
java输入两个正整数A, B,求[A, B]内完全平方数的个数。( 1 ≤ A ≤ B ≤ 10^9 )
以下是Java求[A, B]内完全平方数个数的示例代码:
```java
import java.util.Scanner;
public class CountPerfectSquares {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入正整数A:");
int A = scanner.nextInt();
System.out.print("请输入正整数B:");
int B = scanner.nextInt();
int count = 0;
for (int i = A; i <= B; i++) {
if (isPerfectSquare(i)) {
count++;
}
}
System.out.println("在[" + A + ", " + B + "]内的完全平方数个数为:" + count);
}
// 判断一个数是否为完全平方数
public static boolean isPerfectSquare(int num) {
int sqrt = (int) Math.sqrt(num);
return sqrt * sqrt == num;
}
}
```
阅读全文