平方数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
飞飞特别喜欢平方数,可是他数学并不好,你能帮他计算n与m之间所有平方数之和吗?
提示:若一个整数的开方还是整数,它就是平方数。例如:4、9、16、25是平方数。
Input
第一行 T 代表数据的组数。
接下来有 T 行,每行两个整数n,m (0 <= n, m <= 100000000)
Output
输出一个整数,代表所求区间内平方数之和。
Sample Input
3 1 4 3 10 17 20
Sample Output
5 13 0
Hint
Source
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin = new Scanner(System.in);
int a = cin.nextInt();
int i;
for(i = 0;i < a;i++)
{
int p = 0;
int j,h;
int b = cin.nextInt();
int c = cin.nextInt();
if(c < b)
{
int t;
t = c;
c = b;
b = t;
}
for(j = b;j <= c;j++)//从b到c查找范围内的平方数,使用sqrt函数判断
{
double w = Math.sqrt(j);
int e = (int) w;
if(e * e == j)
p = p + j;
}
System.out.println(p);
}
}
}