java代码,idea中运行起来看起来没什么问题,但是报错WA,求指点
查看原帖
java代码,idea中运行起来看起来没什么问题,但是报错WA,求指点
1444408
AnPotato楼主2024/9/27 18:01
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();//输入操作次数
        Integer x;
        Integer y;
        int value = 2;
        int key = 1;
        Map<Integer,Integer> map = new HashMap<>();
        Main main = new Main();
        for(int i = 0;i < T;i++){
            x = sc.nextInt();//输入x和y
            y = sc.nextInt();
            while(!(map.containsKey(x) && map.containsKey(y))){//寻找第x和y个质数
                if(main.judge(value)){//调用judge判断
                    map.put(key, value);//判断成功,第count个数组值为该质数
                    key++;
                }
                value++;
            }
            main.yiHuo(x, y,map);//判断第x和第y个数的异或值是否为1
        }


    }

    boolean judge(int i) {//判断质数
        if (i <= 1) {
            return false;// 1和负数不是质数
        }
        for (int j = 2; j <= i - 1; j++) {
            if (i % j == 0) {
                return false;//如果出现整除,不是质数
            }
        }
        return true;//没有整除的情况,是质数
    }

    void yiHuo(Integer x,Integer y,Map map){
        // 进行异或运算,判断其结果
        //1.获得其二进制形式的数值
        String no1 = Integer.toBinaryString((Integer) map.get(x));
        String no2 = Integer.toBinaryString((Integer) map.get(y));
        //2.想要异或结果是1,则二进制位数一定要相同
        //并且每位的值都要相同,只能有最后一位不相同
        if(no1.length() != no2.length()){//两个质数二进制位数不同
            System.out.println("No");
        } else if (no1.substring(0,no1.length()-1).equals(no2.substring(0,no2.length()-1))) {//前面数据相等
            if(!(no1.substring(no1.length()-1).equals(no2.substring(no2.length()-1)))){//最后一位不相等
                System.out.println("Yes");
            }
        }
    }
}
2024/9/27 18:01
加载中...