如何比较打印数组索引以查看它们是否为一对

时间:2018-07-09 12:36:45

标签: c++ arrays playing-cards

从卡组中抽出2张牌,并检查它们是否为一对(等级相同)。重复至少1000次,并计算从一副纸牌中抽出一对的可能性。

#include <iostream>
#include <cstdlib> 
#include <cstdio>
#include <string>

using namespace std;

int counter;


string facevalue[] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight","Nine", "Ten", "Jack", "Queen", "King", "Ace" };


string suit [] = { "Diamonds", "Hearts", "Spades", "Clubs" };

string getcard() {
    string card;
    int cardvalue = rand() % 13;
    int cardsuit = rand() % 4;

    card += facevalue[cardvalue];
    card += " of ";
    card += suit[cardsuit];

    if(cardvalue = cardvalue){
        counter = counter + 1;
    }


    return card;
}

int main() {
    int numberofcards = 2;
    int times = 1000;


    for (int y =0; y < times; y++){

    cout<<" "<<endl;

    for (int i = 0; i < numberofcards; i++) {
        cout << "You drew a " << getcard() << endl;

     }
     }

     cout<<"counter: "<<counter<<endl;




} 

因此,在这里,cardvalue控制从数组中打印出的内容。因此,如果cardvalue为1,则facevalue [cardvalue]将为facevalue [1],将打印出“ Two”。

所以现在我要确定从卡组中随机挑选2张卡时,卡值会多少次相同。

我做了

    if(cardvalue = cardvalue){
        counter = counter + 1;
    }

我得到的结果是926,这意味着当从卡组中抽出2张卡1000次时,卡值等于926倍。这似乎不太对劲,如果有人可以纠正我的程序或指导我完成整个过程,将不胜感激。

我尝试过(cardvalue == cardvalue)
但我得到了反数:2000。

2 个答案:

答案 0 :(得分:0)

首先删除全局计数器变量,这是一种不好的做法。

然后您可以尝试类似的方法:

int main() {
    int numberofcards = 2;
    int times = 1000;
    int counter = 0;

    std::map<string, int> cardMap;
    string currentCard;
    int maxNbSameCard;
    for (int y =0; y < times; y++){
       cardMap.clear();
       maxNbSameCard = 0;
       for (int i = 0; i < numberofcards; i++) {
          currentCard = getcard();
          cardMap[currentCard]+=1;

          cout << "You drew a " << currentCard << endl;
          cout << "It is the " << cardMap[currentCard] << " time you get this card" << endl;
          if(maxNbSameCard < cardMap[currentCard]) maxNbSameCard = cardMap[currentCard];
         }
     if(maxNbSameCard > 1) ++counter;
     }

     cout<<"counter: "<<counter<<endl;
} 
  1. 请注意,“ =”是一种情感,而不是一种比较。
  2. 获取对数并不是getcard()的工作,它只是卡的创建者,对于以前创建的卡没有可见性。

答案 1 :(得分:0)

如果直接使用索引而不是字符串,则可能会出现以下情况:

int color_of_card(int i)
{
    return i / 13;
}

int value_of_card(int i)
{
    return i % 13;
}

std::string card_as_string(int i)
{
    static const std::string facevalues[] = {
        "Two", "Three", "Four", "Five", "Six", "Seven",
        "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"
    };
    static const std::string suits[] = { "Diamonds", "Hearts", "Spades", "Clubs" };

    return facevalues[value_of_card(i)] + " of " + suits[color_of_card(i)];
}

int getcard() {
    return rand() % 52;
}

int main() {
    const int times = 1000;

    int counter = 0;
    for (int y = 0; y != times; y++)
    {
        auto card1 = getcard();
        auto card2 = getcard();
        while (card1 == card2) { card2 = getcard(); } // Ensure cards differ.

        if (value_of_card(card1) == value_of_card(card2)) {
            ++counter;
        }
    }
    std::cout << counter << std::endl;  // 58 or 59 normally
    // Once you took a card, there are only 3 card on same value
    // and there is 51 remaining cards.
    std::cout << 3 / 51.f << std::endl; // 0.0588235
}

Demo