无法拯救我的菜-----焦作网络赛 J. Participate in E-sports

地址:https://nanti.jisuanke.com/t/31719

二分判断是否是完全平方数,用java大数写,写完发现超时,得常数优化,能卡过去,就是除2用右移来取代

import java.util.*;
import java.math.*;


public class Main {

	static int judge(BigInteger num)
	{
		if(num.compareTo(BigInteger.ONE) == 0 || num.compareTo(BigInteger.ZERO) == 0)
			return 1;
		BigInteger l = BigInteger.ONE;
		BigInteger r = num;
		while(l.compareTo(r) <= 0)
		{
			//这里用divide就会超
			BigInteger mid = l.add(r).shiftRight(1);
			BigInteger ans = mid.multiply(mid);
			if(ans.compareTo(num) == 0) {
				return 1;
			}else if(ans.compareTo(num) < 0) {
				l = mid.add(BigInteger.ONE);
			}else {
				r = mid.subtract(BigInteger.ONE);
			}
		}
		return 0;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
			int t = cin.nextInt();
			while(t != 0) {
				t--;
				String str = cin.next();
				BigInteger n = new BigInteger(str);
				BigInteger y = n.multiply(n.subtract(BigInteger.ONE)).shiftRight(1);
				BigInteger x = n;
				//System.out.println(x.toString() + " " + y.toString());
				int p = judge(x);
				int q = judge(y);
				//System.out.println(p + " " + q);
				if(p == 1 && q == 1) {
					System.out.println("Arena of Valor");
				}else if(p == 1 && q == 0) {
					System.out.println("Hearth Stone");
				}else if(p == 0 && q == 1) {
					System.out.println("Clash Royale");
				}else {
					System.out.println("League of Legends");
				}
			}
		}

}

猜你喜欢

转载自blog.csdn.net/qq_36386435/article/details/82809957
今日推荐