Square Numbers(Square Numbers)

题目链接
正解是预处理每个数有多少个小于等于自己的平方数记为num【i】,ans(a,b)=num【b】-num【a-1】。
我的解法是用函数暴力求n以内有多少个平方数,然后带入ans(a,b)=num【b】-num【a-1】计算。
附上AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<set>
#include<vector>
#include<map>
#include<string>
#include<cmath>
#define pq priority_queue
#define Pi acos(-1.0)
#define MAXX 1000000007
using namespace std;
int gg(int x)
{
    int ans=0;
    for(int i=1;i*i<=x;i++)
    {
        ans++;
    }
    return ans;
}
int main()
{
    int a,b;
    while(scanf("%d%d",&a,&b),a+b!=0)
    {
        cout<<gg(b)-gg(a-1)<<endl;
    }
    return 0;
}

Memory: 0 KB Time: 0 MS
Language: C++ 4.8.2 Result: Accepted

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Since Long.MAX_VALUE is the largest possible value for a long integer in Java, we cannot calculate the square of any integer greater than or equal to its square root without exceeding Long.MAX_VALUE. Therefore, we need to find the first 5 square numbers that are greater than Long.MAX_VALUE using a different approach. One efficient approach is to use the BigInteger class in Java, which allows us to perform arithmetic operations with arbitrarily large integers. We can create a BigInteger variable initialized with the square root of Long.MAX_VALUE and then increment it until we find the first 5 square numbers that are greater than Long.MAX_VALUE. Here's the Java code to implement this approach: ```java import java.math.BigInteger; public class SquareNumbers { public static void main(String[] args) { BigInteger maxLong = BigInteger.valueOf(Long.MAX_VALUE); BigInteger n = sqrt(maxLong).add(BigInteger.ONE); // start with sqrt(Long.MAX_VALUE) + 1 int count = 0; while (count < 5) { BigInteger square = n.multiply(n); if (square.compareTo(maxLong) > 0) { // if square > Long.MAX_VALUE System.out.println(square); count++; } n = n.add(BigInteger.ONE); } } // helper method to calculate the square root of a BigInteger private static BigInteger sqrt(BigInteger n) { BigInteger a = BigInteger.ONE; BigInteger b = n.shiftRight(5).add(BigInteger.valueOf(8)); while (b.compareTo(a) >= 0) { BigInteger mid = a.add(b).shiftRight(1); if (mid.multiply(mid).compareTo(n) > 0) { b = mid.subtract(BigInteger.ONE); } else { a = mid.add(BigInteger.ONE); } } return a.subtract(BigInteger.ONE); } } ``` The sqrt() method is a helper method to calculate the square root of a BigInteger using the binary search algorithm. The main() method initializes n with the square root of Long.MAX_VALUE plus one, and then uses a while loop to increment n and check if its square is greater than Long.MAX_VALUE. Once we find 5 such square numbers, we print them out and exit the program.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值