Algorithm Note _184: Previous questions about several times selected cards (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

  1 problem description Problem Descriptionin his spare time, Sherlock Holmes and Watson played a game:
there are n integers written on the n card. The two men took turns to take a card. The number required for the next person must be an approximate or multiple of the number that the previous person took. For example, the number "6" is written on a card taken by Sherlock Holmes, and then the figures that Watson can take include:
6,12,18,24, ....
when it is time for a party to pick up a card, the card that does not meet the requirements is optional, and the party is the loser.
Please take advantage of the advantages of the computer to calculate, in the known all cards on the number and the optional number of the conditions, how to choose to ensure victory!
when multiple numbers are selected to win, the smallest number is output. If you lose anyway, output-1. Input Formatthe input data is 2 rows. The first line is a number of spaces separated by integers (each integer between 1~100), representing all the cards that are currently remaining.
The second line is also an integer separated by a number of spaces, indicating the number that can be selected. Of course, the number in the second row must be fully contained in the first row of numbers. output FormatThe program is output to win the Gongfu!! Sample Input2 3 6
3 6Sample Output3Sample Input1 2 2 3 3 4 5
3 4 5Sample Output4 

2 Solutions

The specific code is as follows:

Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Scanner; Public classMain { Public intDfsint[] NumA, arraylist<integer>[] ListA,intstart) {         for(inti = Lista[start].size ()-1;i >= 0;i--) {            if(Numa[lista[start].get (i)] > 0) {numa[lista[start].get (i)]--; intt =dfs (NumA, ListA, Lista[start].get (i)); Numa[lista[start].get (i)]++; if(t = =-1)                    return1; }            }        return-1; }             Public voidGetResult (String A, String B) {int[] NumA =New int[105]; @SuppressWarnings ("Unchecked") ArrayList<integer>[] ListA =Newarraylist[105];  for(inti = 1;i < 105;i++) Lista[i]=NewArraylist<integer>(); ArrayList<Integer> Listb =NewArraylist<integer>(); String[] Arraya= A.split (""); String[] Arrayb= B.split ("");  for(inti = 0;i < arraya.length;i++) {            intA =integer.valueof (Arraya[i]); Numa[a]++; }         for(inti = 1;i <= 100;i++) {            if(numa[i]! = 0) {Numa[i]--;  for(intj = 1;j <= 100;j++)                    if(numa[j]! = 0 && (i% j = 0 | | J% i = = 0) ) Lista[i].add (j); Numa[i]++; }        }         for(inti = 0;i < arrayb.length;i++) {            intb =integer.valueof (Arrayb[i]);        Listb.add (b);        } collections.sort (LISTB);  for(inti = 0;i < Listb.size (); i++) {numa[listb.get (i)]--; intt =dfs (NumA, ListA, Listb.get (i)); Numa[listb.get (i)]++; if(t = =-1) {System.out.println (Listb.get (i)); return; }} System.out.println ("-1"); return; }         Public Static voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); String A=In.nextline (); String B=In.nextline ();    Test.getresult (A, B); }}

Resources:

1. Blue Bridge Cup previous questions--about several times the choice of cards (game theory +dfs)

Algorithm Note _184: Previous questions about several times selected cards (Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.