每行有两个不大于65535的非负整数
对于每一行的两个整数,输出一行,内容为YES或NO
2 4 9 18 45057 49158 7 12
YES YES YES NO
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String line = in.nextLine(); String[] nums = line.split(" "); int n1 = Integer.valueOf(nums[0]); int n2 = Integer.valueOf(nums[1]); if (n1 == n2) { System.out.println("YES"); } else { // 1000000000000000 32768 int num = Integer.parseInt("1000000000000000", 2);// 2表示进制 int count = 0; boolean find = false; while (count < 16) { if (n1 >= num) { // 表示n2最左位是1 // 先把最左位变成0 n1 = n1 ^ num;// 两个数相同异或,则变成0,任何数异或0都等于它本身 // 再左移1位 n1 = n1 << 1; // 再给右位加1 n1 = n1 + 1; } else {// 如果最左位是0,则和普通左移没区别 n1 = n1 << 1; } if (n1 == n2) { find = true; break; } count++; } if (find) { System.out.println("YES"); } else { System.out.println("NO"); } } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner reader = new Scanner(System.in); int a = reader.nextInt(); int b = reader.nextInt(); String binary_a = convert(a); String binary_b = convert(b); for (int j = 0; j < binary_b.length(); ++j) { int last = j; int i = 0; while (binary_a.charAt(i) == binary_b.charAt(last)) { i = (i+1)%binary_a.length(); last = (last+1)%binary_b.length(); if (last == j) { System.out.println("YES"); return; } } } System.out.println("NO"); } public static String convert(int a) { StringBuilder sb = new StringBuilder(); while (a > 0) { sb.insert(0, String.valueOf(a%2)); a /= 2; } int len = sb.length(); for (int i = 0; i < 16-len; ++i) sb.insert(0, "0"); return sb.toString(); } }