第五届在线编程大赛月赛第一题:完全平方数的个数
/* 题目详情: 给定整数区间[A,B]问其中有多少个完全平方数。 输入格式: 多组数据,包含两个正整数A,B 1<=A<=B<=2000000000。 输出格式: 每组数据输出一行包含一个整数,表示闭区间[A,B]中包含的完全平方数的个数。 答题说明: 输入样例 1 1 1 2 3 10 3 3 输出样例: 1 1 2 0 */ #include<iostream> #include<stdio.h> #include<math.h> using namespace std; typedef __int64 LL; int getResult(LL a, LL b) { LL i,sum,ans; i=(int)sqrt(double(a)); if(i*i==a) sum=1; else sum=0; i++; ans=i*i; while(ans<=b) { sum++; i++; ans=i*i; } return sum; } int main() { LL a,b; while(scanf("%I64d%I64d",&a,&b)!=EOF) { printf("%d\n",getResult(a,b)); } return 0; }