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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章