J: Participate in E-sports [大数牛顿迭代判断是否是平方数]

J: Participate in E-sports [大数牛顿迭代判断是否是平方数]

**题目描述
Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don’t know which one to choose, so they use a way to make decisions.
They have several boxes of candies, and there are i candies in the i^th box, each candy is wrapped in a piece of candy paper. Jessie opens the candy boxes in turn from the first box. Every time a box is opened, Jessie will take out all the candies inside, finish it, and hand all the candy papers to Justin.
When Jessie takes out the candies in the N^th box and hasn’t eaten yet, if the amount of candies in Jessie’s hand and the amount of candy papers in Justin’s hand are both perfect square numbers, they will choose Arena of Valor. If only the amount of candies in Jessie’s hand is a perfect square number, they will choose Hearth Stone. If only the amount of candy papers in Justin’s hand is a perfect square number, they will choose Clash Royale. Otherwise they will choose League of Legends.
Now tell you the value of N, please judge which game they will choose.

输入
The first line contains an integer T(1≤T≤800), which is the number of test cases.
Each test case contains one line with a single integer: N(1≤N≤10^200).

输出
For each test case, output one line containing the answer.**

在这里插入图片描述


import java.math.BigInteger;
import java.math.BigDecimal;
import java.util.Scanner;

public class main {
    public static void main(String[] args) {
    	
    	Scanner scan =new Scanner(System.in);
    	int n = scan.nextInt();
    	BigInteger justin = new BigInteger("0");
    	//BigInteger jessie = new BigInteger("0");
    	while(n!=0)
    	{
    	BigInteger jessie = scan.nextBigInteger();
        boolean w = is_square(jessie);
        boolean x = is_square(justin);
        if( w==true && x == true )
            System.out.println("Arena of Valor");
        else if(w == true && x == false )
            System.out.println("Hearth Stone");
        else if(w == false && x == true)
            System.out.println("Clash Royale");
        else
        	System.out.println("League of Legends");
        justin =  justin.add(jessie);
        n--;
    	}

    }

    // 判断是否为完全平方数
    public static boolean is_square(BigInteger sqrttest){
        // 牛顿法求解平方根, 求解a的平方根
        // x为a的平方根,x的初始值为1, 按x = (x+a/x)/2迭代, 误差为error
        BigDecimal x = BigDecimal.ONE;
        BigDecimal a = new BigDecimal(sqrttest.toString());
        BigDecimal eps = new BigDecimal("1");
        final BigDecimal error = new BigDecimal("1E-10");
        int scale = 100;

        // 进入循环
        while(eps.compareTo(error) == 1){ // eps > error
            x = x.add(a.divide(x, scale, BigDecimal.ROUND_HALF_UP)).divide(new BigDecimal("2.0"), scale, BigDecimal.ROUND_HALF_UP);
            eps = x.multiply(x).subtract(a).abs();
        }

        BigInteger sqrt = x.toBigInteger();  // 求平方根的整数部分
        if(sqrt.pow(2).compareTo(sqrttest) == 0)
            return true;
        else
            return false;

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RockyBlog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值