uva11461 Square Numbers

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

猜你喜欢

转载自blog.csdn.net/zangfong/article/details/82793461