如何平均对的百分比

时间:2015-06-24 23:47:12

标签: c++ visual-c++

我被困在我的最后一段代码上。我知道我必须在每次试验中添加我所有的配对百分比,但我如何在每次试验中添加所有配对百分比?另外,我如何只在输出中显示为“平均百分比=”?

#include <iostream>
#include <iomanip>
#include "card.h"
#include "deck.h"
#include "stdlib.h"
#include "time.h"

using namespace std;


int main(){

srand((unsigned)time(NULL)); //randomize function




for (int y = 1; y <= 10; y++){//this gives me 10 trials 

    int numberOfPairs = 0;
    int numberOfFlushes = 0;
    int numberOfHands = 10000;

    for (int i = 0; i < numberOfHands; i++){// this gives me 10,000 of whatever I put inside this loop


        Deck gameDeck;//(calls the constructor) this creates the deck 
        gameDeck.shuffle(100);//this shuffles the deck 100 times 

        Card hand[5];//defining five cards
        hand[0] = gameDeck.getCard(); //listing the cards 1-5
        hand[1] = gameDeck.getCard();
        hand[2] = gameDeck.getCard();
        hand[3] = gameDeck.getCard();
        hand[4] = gameDeck.getCard();

        Card::Suit checkFlush = hand[0].getSuit(); //get the suit of the first card starting at zero
        bool hasFlush = true;
        bool hasPair = false;

        for (int j = 0; j < 5; j++){ //runs through the deck (checking for pairs)
            if (hand[j].getSuit() != checkFlush){//this is comparing the j series with the suites 
                hasFlush = false; //do not redefine 
            }
            for (int k = 0; k < 5; k++){//runs through the deck and compares j and k

                if (j != k && !hasPair){// dont want them to equal because its comparing the same card && search for a pair if you havent found one
                    if (hand[j].getValue() == hand[k].getValue()){//this tells us we have a pair
                        numberOfPairs++;//keeping track of my pairs 
                        hasPair = true;
                        break;//breaks after finding a pair
                    }

                }

            }
        }
        if (hasFlush == true){
            numberOfFlushes++;//keeps track of my flushes
        }


    }
    float percentagePair = ((float)numberOfPairs / numberOfHands) * 100.0; //finds percentage with a least a pair for each trial
    float percentageFlush = ((float)numberOfFlushes / numberOfHands) * 100.0;//finds percentage with a least a flush for each trial
    float avgPercentagePair = (percentagePair/ 10); // takes the average percent of pairs of all 10 trials



    std::cout << "Trial number = " << y << endl;
    std::cout << "Hands dealt = "  << numberOfHands << endl;
    std::cout << "Number of hands with at least a pair = " << numberOfPairs << endl;
    std::cout << "Number of hands with a flush = " << numberOfFlushes << endl;
    std::cout << "Percentage of hands with at least a pair = " << percentagePair << "%" << endl;
    std::cout << "Percentage of hands with a flush = " << percentageFlush << "%" << endl << endl;

    std::cout << "Average Percentage of pairs = " << avgPercentagePair << "%" << endl;

1 个答案:

答案 0 :(得分:0)

每次试用后保持百分之百的运行总和。在最后一次试验之后,将运行总和除以试验次数。

正如您的代码现在所示,avgPercentagePair只占百分比的10%。