平方数(Java)

平方数

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic Discuss

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);
		}
	}

}

 


版权声明:本文为qq_41298570原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。