Participate in E-sports【Java大数+二分】
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Participate in E-sports【Java大数+二分】,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3766字,纯文字阅读大概需要6分钟。
内容图文
![Participate in E-sports【Java大数+二分】](/upload/InfoBanner/zyjiaocheng/740/78433d6878d34a4c96e49363efda73ff.jpg)
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");
}
}
}
内容总结
以上是互联网集市为您收集整理的Participate in E-sports【Java大数+二分】全部内容,希望文章能够帮你解决Participate in E-sports【Java大数+二分】所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。