C语言编程题:平方数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40688217/article/details/90245079

平方数

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


参考代码:

#include<stdio.h>
#include<math.h>
int main()
{
	int N,m,n,temp,sum;
	scanf("%d",&N);
	while(N--){
		scanf("%d%d",&n,&m);
		sum=0;
		if(n>m){           //置换成从小到大,便于##处定初始位置
			temp=n;
			n=m;
			m=temp;
		}
	
		for(int k=n;k<=m;k++){      //##
			if(sqrt(k)==(int)sqrt(k))  //平方数判定条件
				sum+=k;
		}
		printf("%d\n",sum);	
	}
		

}

猜你喜欢

转载自blog.csdn.net/weixin_40688217/article/details/90245079