Participate in E-sports【Java大数+二分】
作者:互联网
Participate in E-sports
时间限制: 2 Sec 内存限制: 128 MB
提交: 194 解决: 53
[提交] [状态] [命题人:admin]
题目描述
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.
样例输入
复制样例数据
4
1
2
3
4
样例输出
Arena of Valor
Clash Royale
League of Legends
Hearth Stone
题目大意:
输入一个数n,记录另一个n1=1+2+3+⋯+(n−1)=2n×(n−1)
完全平方数有:0(02),1(12),4(22),9(32),16(42)…
若n为完全平方数,则记ju1=true;
若n1为完全平方数,则记ju2=true;
若ju1==true&&ju2==true,输出 “Arena of Valor”
若ju1==true&&ju2==false,输出 “Hearth Stoner”
若ju1==false&&ju2==true,输出 “Clash Royale”
若ju1==false&&ju2==false,输出 “League of Legends”
解题思路:
由于此题n很大,所以可以使用Java中的大数来写,而判断一个数是否为完全平方数,可以通过二分来判断。
代码:
import java.math.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO code application logic here
Scanner cin=new Scanner(System.in);
int t=0;
t=cin.nextInt();
boolean ju1,ju2;
while(t!=0) {
t--;
BigInteger a=BigInteger.ZERO;
a=cin.nextBigInteger();
BigInteger b=BigInteger.ZERO;
b=a.subtract(BigInteger.ONE);
b=b.multiply(a);
BigInteger c=BigInteger.ONE;
c=c.add(c);
b=b.divide(c);
ju1=false;ju2=false;
if(a.compareTo(BigInteger.ONE)==0) {
ju1=true;ju2=true;
}
else {
BigInteger l=BigInteger.ONE;
BigInteger r=a;
BigInteger mid=BigInteger.ZERO;
while(r.compareTo(l)>=0) {
mid=l.add(r);
mid=mid.divide(c);
//System.out.println(l+" "+r+" "+mid);
BigInteger nape=BigInteger.ZERO;
nape=mid.multiply(mid);
if(nape.compareTo(a)==0) {
ju1=true;
break;
}
else if(nape.compareTo(a)==1) {
r=mid.subtract(BigInteger.ONE);
}
else {
l=mid.add(BigInteger.ONE);
}
}
l=BigInteger.ONE;
r=b;
while(r.compareTo(l)>=0) {
mid=l.add(r);
mid=mid.divide(c);
BigInteger nape=BigInteger.ZERO;
//System.out.println(l+" "+r+" "+mid);
nape=mid.multiply(mid);
if(nape.compareTo(b)==0) {
ju2=true;
break;
}
else if(nape.compareTo(b)==1) {
r=mid.subtract(BigInteger.ONE);
}
else {
l=mid.add(BigInteger.ONE);
}
}
}
if(ju1&&ju2) System.out.println("Arena of Valor");
if(ju1&&!ju2) System.out.println("Hearth Stone");
if(!ju1&&ju2) System.out.println("Clash Royale");
if(!ju1&&!ju2) System.out.println("League of Legends");
}
}
}
标签:BigInteger,Java,mid,sports,ju2,ju1,Participate,amp,true 来源: https://blog.csdn.net/YT201758501112/article/details/98313938