Participate in E-sports【Java大数+二分】
Participate in E-sports
時(shí)間限制: 2 Sec 內(nèi)存限制: 128 MB
提交: 194 解決: 53
[提交] [狀態(tài)] [命題人: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.
樣例輸入
復(fù)制樣例數(shù)據(jù)
4
1
2
3
4
樣例輸出
Arena of Valor
Clash Royale
League of Legends
Hearth Stone
題目大意:
輸入一個(gè)數(shù)nnn,記錄另一個(gè)n1=1+2+3+?+(n?1)=n×(n?1)2n1= 1+2+3+\dots+(n-1)=\cfrac{n\times(n-1)}{2}n1=1+2+3+?+(n?1)=2n×(n?1)?
完全平方數(shù)有:0(02),1(12),4(22),9(32),16(42)…0(0^2),1(1^2),4(2^2),9(3^2),16(4^2)\dots0(02),1(12),4(22),9(32),16(42)…
若nnn為完全平方數(shù),則記ju1=trueju1=trueju1=true;
若n1n1n1為完全平方數(shù),則記ju2=trueju2=trueju2=true;
若ju1==true&&ju2==trueju1==true \&\& ju2==trueju1==true&&ju2==true,輸出 “Arena of Valor”
若ju1==true&&ju2==falseju1==true \&\& ju2==falseju1==true&&ju2==false,輸出 “Hearth Stoner”
若ju1==false&&ju2==trueju1==false \&\& ju2==trueju1==false&&ju2==true,輸出 “Clash Royale”
若ju1==false&&ju2==falseju1==false \&\& ju2==falseju1==false&&ju2==false,輸出 “League of Legends”
解題思路:
由于此題nnn很大,所以可以使用Java中的大數(shù)來(lái)寫(xiě),而判斷一個(gè)數(shù)是否為完全平方數(shù),可以通過(guò)二分來(lái)判斷。
代碼:
import java.math.*; import java.util.*;public class Main {public static void main(String[] args) {// TODO code application logic hereScanner 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");}} }總結(jié)
以上是生活随笔為你收集整理的Participate in E-sports【Java大数+二分】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 山东省第八届 ACM 省赛 sum of
- 下一篇: 排名前16的Java工具类