#include #include #include #include #include using namespace std; /* Arrays mapping characters to the card values they represent. */ string keys = "23456789TJQKA"; // 2 3 4 5 6 7 8 9 T J Q K A int values[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11}; //Deal one card, to either the player or the dealer. void dealCard(int& hand, bool& hasEleven, int card) { if (card == 11) //Dealing an ace { //Already using an ace as 11, so the new ace must be 1 (since 11 + 11 = 22) if (hasEleven) hand++; //Otherwise count the ace as 11 else { hand += 11; hasEleven = true; } } else //Dealing any card other than an ace hand += card; //If the hand is over 21 but we have an ace counted as 11, change the ace to 1. if (hand > 21 && hasEleven) { hand -= 10; hasEleven = false; } } int main() { ifstream infile("nowin.in"); ofstream outfile("nowin.out"); while (true) { vector deck; string str; getline(infile, str); if (!(infile.good())) { return 0; } //Form the deck out of the input string for (int i = 0; i < str.length(); i++) { int index = keys.find(str[i]); if (index != string::npos) { deck.push_back(values[index]); } } //The value of the player/dealer's hand int playerHand = 0; int dealerHand = 0; //True if the player/dealer has an ace counted as 11 bool playerEleven = false; bool dealerEleven = false; //Deal the first 4 cards. dealCard(playerHand, playerEleven, deck[0]); dealCard(dealerHand, dealerEleven, deck[1]); dealCard(playerHand, playerEleven, deck[2]); dealCard(dealerHand, dealerEleven, deck[3]); int oldDealerHand = dealerHand; bool oldDealerEleven = dealerEleven; bool canWin = false; //Is it possible for the player to win? if (playerHand == 21) //If the player starts out with 21, automatically wins. canWin = true; int dealerStart = 4; //the index in the deck where the dealer will start drawing //In this loop, try each possible number of cards the player could draw. while (!canWin && playerHand <= 21) { //Dealer's turn is at beginning of loop, to allow for player drawing 0 cards int curCard = dealerStart; //Reset the dealer hand to what it was after the first 4 cards. dealerHand = oldDealerHand; dealerEleven = oldDealerEleven; //The dealer draws cards here. while (dealerHand < 17) { dealCard(dealerHand, dealerEleven, deck[curCard]); curCard++; } if (dealerHand <= playerHand || dealerHand > 21) canWin = true; //Try adding another card to the player's hand dealCard(playerHand, playerEleven, deck[dealerStart]); dealerStart++; } if (canWin) outfile << "Yes" << endl; else outfile << "No" << endl; } return 0; }