蓝桥杯JAVA版答案——历年真题——约数倍数选卡片

更多JAVA版答案移步我的博客:蓝桥杯JAVA版答案汇总

本题考查

博弈论

思路

AC代码

import java.util.LinkedList;
import java.util.Scanner;
public class Main {
     
	static int[] card = new int[101];
	@SuppressWarnings("unchecked")
	static LinkedList<Integer>[] num = new LinkedList[101];
	static boolean judge(int pre) {
     
		card[pre]--;
		for(int i=num[pre].size()-1; i>=0; i--) {
     
			int temp = num[pre].get(i);
			if(card[temp]>0)
				if(!judge(temp))	{
     card[pre]++;	return true;}
		}
		card[pre]++;
		return false;
	}
	public static void main(String[] args) {
     
		Scanner scaner = new Scanner(System.in);
		String[] tempArr1 = scaner.nextLine().split(" ");
		String[] tempArr2 = scaner.nextLine().split(" ");
		int[] first = new int[tempArr2.length];
		for(int i=0; i<tempArr1.length; i++)	card[Integer.parseInt(tempArr1[i])]++;
		for(int i=0; i<tempArr2.length; i++)	first[i] = Integer.parseInt(tempArr2[i]);
		scaner.close();
		for (int i = 1; i < 101; i++) {
     
			num[i] = new LinkedList<Integer>();
			if (card[i]>0) {
     
				card[i]--;
				for (int j = 1; j < 101; j++)
					if (card[j]>0 && (j%i==0||i%j==0))	num[i].add(j);
				card[i]++;
			}
		}
		for(int i=0; i<first.length; i++)	if(!judge(first[i]))	{
     System.out.println(first[i]);	return;}
		System.out.println("-1");
	}
}

你可能感兴趣的:(蓝桥杯,JAVA,java)