平方数

平方数

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

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int t;
    int n,m;
    int i;
    int temp;
    int sum;
    scanf("%d",&t);
    while(t--)
    {
        sum=0;
        scanf("%d %d",&n,&m);
        if(n>m)
        {
            temp=n;
            n=m;
            m=temp;
        }
        for(i=n;i<=m;i++)
        {
            temp=sqrt(i);//sqrt是平方根
            if(temp*temp==i)
                sum+=i;
        }
        printf("%d\n",sum);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值