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