Coin in line 变形

从list里面,先后取数,先达到target的获胜,判断先手能否获胜。

    public static void main(String[] args) {

       LinkedList<Integer> n = new LinkedList<>();
       n.add(1);
       n.add(2);
       n.add(3);
       n.add(4);
       n.add(5);
      boolean res = memorySearch(n, 0, 11);
      System.out.println(res);
    }


    public static boolean memorySearch(LinkedList<Integer> n, int sum, int target) { // 0 is empty, 1 is false, 2 is true

        boolean result = true;
        if(n.size()==0)
            return false;
        else if(n.size()==1)
            return true;
        else{
            for(int i = 0; i<n.size();i++){
                if(n.get(i)+sum>target) return true;
                int num = n.remove(i);
                if(memorySearch(n, sum+num,target)) result = false;
                n.add(num);
            }

        return result;
        }
    }

results matching ""

    No results matching ""