如何改组数组问题?

时间:2016-03-15 07:06:16

标签: c++ arrays

我刚刚接触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"};

感谢提前!

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

正如问题评论中所述,您可以使用rand()中的cstdlib并限制返回的随机数,请使用modulus(%)

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

然后使用该索引访问数组中的问题。

cout << questionpart[index] << endl;

编辑:您可能需要使用ctime作为srand

EDIT2:在不重复相同问题的情况下进行随机化,您可能必须存储已经用于跟踪它的问题,或者只是在您不再需要时将其从阵列中完全删除。

对于更高级的方法,您可以定义一个包含所有内容的对象(问题,答案和状态) 类似的东西:

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

然后从中创建一个数组(或者最好vector用于动态数组支持)

Question questions[array_size];
相关问题