👨🏫 Rikka with Square Numbers
🧀 参考题解
🍻 AC code
import java.util.Scanner;
public class Main
{
static boolean isSqu(int x)
{
int t = (int) Math.sqrt(x);
return t * t == x;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-- > 0)
{
int a = sc.nextInt();
int b = sc.nextInt();
int d = (a - b) > 0 ? a - b : b - a;
if (isSqu(d))
System.out.println(1);
else if (d % 4 == 0 || d % 2 == 1)
{
System.out.println(2);
} else
{
boolean flag = false;
for (int i = 1; i * i < d; i++)
if (isSqu(d - i * i))
flag = true;
if (flag)
System.out.println(2);
else
{
System.out.println(3);
}
}
}
}
}