UVa 11461 - Square Numbers (水题)

该博客介绍了一个算法问题,即在给定的两个整数a和b之间(包括a和b),找出有多少个完全平方数。计算方法是将b的平方根减去a-1的平方根,结果向下取整即为答案。博客提供了样例输入和输出,并强调在计算过程中必须将两数的平方根转换为整数。

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

                          A                      

Square Numbers

Input: Standard Input

                          Output: Standard Output                     


 

A square number is an integer number whose square root is also an integer. For example 1, 4, 81 are some square numbers. Given two numbers a and b you will have to find out how many square numbers are there between a and b (inclusive).

 

Input

The input file contains at most 201 lines of inputs. Each line contains two integers a and b (0<a≤b≤100000). Input is terminated by a line containing two zeroes. This line should not be processed.

 

Output

For each line of input produce one line of output. This line contains an integer which denotes how many square numbers are there between a and b (inclusive).

 

Sample Input                             Output for Sample Input

1 4
1 10
0 0

2

3

 


Problem Setter: Shahriar Manzoor



题意:

如果一个数可以携程一个整数的平方,则说它是一个完全平方数。输入正整数a,b,有多少个完全平方数介于ab之间。


之间就是(int)sqrt(b) - (int)sqrt(a-1)一定要先两个都转换为int才能相减



#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

int main() {
    int s, t;

    while(scanf("%d%d", &s, &t) != EOF && (s||t)) {
        int ans = (int) sqrt(t) - (int) sqrt(s-1);
        printf("%d\n", ans);
    }

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值