uva11461 Square Numbers

本文介绍了解决UVA11461问题的两种方法,分别使用Python和C++实现。任务是计算在给定区间内完全平方数的数量,提供了简洁高效的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

uva11461

uDebug11461

也是简单题,题意是给定的两个数a和b(0<a<=b<=100000),求a和b之间有多少个完全平方数。

python版本代码

import math

while True:
	a,b = input().split()
	a = int(a)
	b = int(b)
	if a == 0 and b == 0:
		break;
	ans = math.floor(math.sqrt(b)) - math.floor(math.sqrt(a-1))
	print(ans)

C++版本代码,看来是我以前想太多了。。。

#include <iostream>
#include<cstdio>
#include<cmath>
using namespace std;

//#define ZANGFONG
const int maxn = 100010;
int s[maxn];

int main()
{
    #ifdef ZANGFONG
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif // ZANGFONG
    int i,j,num,from,to,a,b;

    from = to = num = 0;
    for(i = 1; i <= sqrt(maxn); i++)
    {
        to = i * i;
        for(j = from; j < to; j++) s[j] = num;
        from = to;
        num++;
    }


    while(scanf("%d%d\n",&a,&b)&&a+b)
    {
        printf("%d\n",s[b]-s[a-1]);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值