繁体   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