如何比较打印的数组索引以查看它们是否成对
How do I compare printed array index to see if they're a pair
从牌组中抽取 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)
但我得到了 counter : 2000 。
首先删除您的全局计数器变量,这是一种不好的做法。
那么你可以尝试类似的东西:
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;
}
- 请注意,“=”是一种矫饰而非比较。
- 计算对数不是 getcard() 的工作,它只是一个卡片创建者,它对以前创建的卡片没有可见性。
如果你直接使用索引而不是字符串,你可能会得到类似的东西:
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
}
从牌组中抽取 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)
但我得到了 counter : 2000 。
首先删除您的全局计数器变量,这是一种不好的做法。
那么你可以尝试类似的东西:
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;
}
- 请注意,“=”是一种矫饰而非比较。
- 计算对数不是 getcard() 的工作,它只是一个卡片创建者,它对以前创建的卡片没有可见性。
如果你直接使用索引而不是字符串,你可能会得到类似的东西:
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
}