簡體   English   中英

如何改組數組問題?

[英]How to shuffle array questions?

我只是C ++的新手。 當我提示時,有人可以幫我把這個問題隨機化嗎? 有一種方法可以隨機化嗎?

我有這個代碼。 有人可以告訴我如何將這些隨機化嗎?

string questionpart[20]={"What is the square root of 4?",
                            "What is the square root of 6?",
                            "What is the square root of 16?",
                            "What is the square root of 25?",
                            "What is the square root of 36?",
                            "What is the square root of 42?",
                            "What is the square root of 48?",
                            "What is the square root of 81?",
                            "What is the square root of 100?",
                            "What is the square root of 121?",
                            "What is the square root of 144?",
                            "What is the square root of 169?",
                            "What is the square root of 196?",
                            "What is the square root of 225?",
                            "What is the square root of 256?",
                            "What is the square root of 289?",
                            "What is the square root of 324?",
                            "What is the square root of 361?",
                            "What is the square root of 400?",
                            "What is the square root of 1?",
                            };

string partans[20]={"2",
                        "3",
                        "4",
                        "5",
                        "6",
                        "7",
                        "8",
                        "9",
                        "10",
                        "11",
                        "12",
                        "13",
                        "14",
                        "15",
                        "16",
                        "17",
                        "18",
                        "19",
                        "20",
                        "1"};

提前致謝!

您可以創建索引向量,並為其使用std::shuffle

您也可以使用半手動洗牌。 例:

srand(time(NULL));
rand();

unsigned indexes[cnt];
unsigned fact = 0;

while(fact < cnt)
{
    const unsigned r = rand() % cnt;
    bool was = false;
    for(unsigned i = 0; i < fact; ++i)
    {
        if(indexes[i] == r) {
            was = true;
            break;
        }
    }
    if(!was)
    {
        indexes[fact] = r;
        ++fact;
    }
}

for(unsigned i = 0; i < cnt; ++i)
{
    const unsigned j = indexes[i];
    cout << "Q: " << questions[j] << "; A: " << answers[j] << endl;
}

如對問題的評論中所述,您可以使用cstdlib中rand()來限制返回的隨機數,請使用系數(%)

srand(time(NULL));
index = rand() % array_size;

然后使用那個索引訪問數組中的問題。

cout << questionpart[index] << endl;

編輯:您可能需要使用ctime作為標記

EDIT2:要隨機化而不重復相同的問題,您可能必須存儲已經用於跟蹤它的問題,或者如果不再需要它,則可以將其從陣列中完全刪除。

對於更高級的方法,您可以定義一個包含所有內容(問題,答案和狀態)的對象,例如:

class Question {
    string question;
    string answer;
    bool wasAsked = false;
}

然后從中創建一個數組(或者最好是向量,以支持動態數組)

Question questions[array_size];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM