这是我到目前为止所做的,我尽力标记代码的每一位。还应该注意,这是用XCode编写的,所以它在Mac上运行。
/*
Ayush Sharma
4 November 2016
*/
#include <iostream>
#include <ctime>
using namespace std;
int main (){
//clearing the screen
system("clear");
//seeding the random
srand((unsigned int)time(NULL));
//variables & arrays
char answer;
int r, m, correct = 0;
string capitals[50] =
{"Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento", "Denver", "Hartford", "Dover", "Tallahassee", "Atlanta", "Honolulu", "Boise", "Springfield", "Indianapolis", "Des Moines", "Topeka", "Frankfort", "Baton Rouge", "Augusta", "Annapolis", "Boston", "Lansing", "St. Paul", "Jackson", "Jefferson City", "Helena", "Lincoln", "Carson City", "Concord", "Trenton", "Santa Fe", "Albany", "Raleigh", "Bismarck", "Columbus", "Oklahoma City", "Salem", "Harrisburg", "Providence", "Columbia", "Pierre", "Nashville", "Austin", "Salt Lake City", "Montpelier", "Richmond", "Olympia", "Charleston", "Madison", "Cheyenne"};
string states[50] = {"Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"};
//title
cout << "***************************************************************\n";
cout << "* *\n";
cout << "* United States Capitals Quiz *\n";
cout << "* *\n";
cout << "***************************************************************\n\n";
for (int i = 0; i < 15; i++){
//Picking A Random State
r = rand() % 50;
//Checking if State is a Repeat
if (states[r] != "-1") {
cout << "What is the capital of " << states[r] << "? ";
//Picking Correct Answer Choice and Respective Layout
m = rand() % 4;
if (m == 0) {
cout << "\nA: " << capitals[r] << endl;
cout << "B: " << capitals[rand()%50] << endl;
cout << "C: " << capitals[rand()%50] << endl;
cout << "D: " << capitals[rand()%50] << endl;
}
if (m == 1) {
cout << "\nA: " << capitals[rand()%50] << endl;
cout << "B: " << capitals[r] << endl;
cout << "C: " << capitals[rand()%50] << endl;
cout << "D: " << capitals[rand()%50] << endl;
}
if (m == 2) {
cout << "\nA: " << capitals[rand()%50] << endl;
cout << "B: " << capitals[rand()%50] << endl;
cout << "C: " << capitals[r] << endl;
cout << "D: " << capitals[rand()%50] << endl;
}
if (m == 3) {
cout << "\nA: " << capitals[rand()%50] << endl;
cout << "B: " << capitals[rand()%50] << endl;
cout << "C: " << capitals[rand()%50] << endl;
cout << "D: " << capitals[r] << endl;
}
//Recieving Answer
cout << "Answer: ";
cin >> answer;
//Converting Letter to Number
if (answer == 'A' || answer == 'a') answer = 0; if (answer == 'B' || answer == 'b') answer = 1;
if (answer == 'C' || answer == 'c') answer = 2; if (answer == 'D' || answer == 'd') answer = 3;
//Comparing Answer to Correct Answer
if (m == answer) {
cout << "Correct!" << endl << endl;
correct++;
}else{
cout << "Incorrect! The correct answer was " << capitals[r] << "! \n\n";
}
//Removing State from Array
states[r] = "-1";
}else{
//If State was a Repeat, generate another State
i--;
}
}
//Printing Results
cout << "Number Correct: " << correct << "/15 or " << ((correct/15.00)*100) << "%!\n";
return 0;
}
代码几乎可以工作。问题是答案有时会重复,例如在场景中: 威斯康星州的首都是什么? A.麦迪逊 B.法兰克福 C.杰克逊 D.麦迪逊 只有A或D才是&#34;正确的&#34;尽管两个文本都有相同的文字(尽管我很难让答案重复)。我还想知道是否有更有效的方法来创建多项选择题的布局。提前谢谢!
-Ayush
答案 0 :(得分:2)
假设您想要随机绘制50
个值而不重复,只需创建一个包含这些值的数组或向量,将其随机播放,然后按顺序访问混洗数组的元素。 / p>
在C ++ 11中,使用来自std::iota()
的算法std::random_shuffle()
和<algorithm>
很容易。
int value[50];
std::iota(std::begin(value), std::end(value), 0); // populate array with values 0 to 49
std::random_shuffle(std::begin(value), std::end(value));
然后在您的外部循环中,而不是r = rand()%50
使用r=value[i]
。
std::begin()
和std::end()
位于标准标题<iterator>
中。
在C ++ 11之前可以使用相同的想法,但方法略有不同(C ++ 11不支持std::begin()
,std::end()
或std::iota()
,但等效物很容易实现)。
我将value
创建为std::vector<int>
而不是var myList = document.getElementsByTagName("a");
var url = "http://www.site2.com";
for (i = 0; i < myList.length; i++) {
myurl = url + myList[i].getAttribute('href');
//alert("the new link is " + url);
myList[i].setAttribute('href', myurl);
}
,而不是{{1}}。我上面使用数组进行了说明,因为你似乎默认使用数组。
答案 1 :(得分:1)
这是一件非常明显的事情。一个简单的解决方案是创建一个数组来保存已经显示的选项。使用while循环向数组添加唯一选项。您可以使用其他函数检查数组中是否有任何重复。然后,显示capitals[r]
以及阵列中的其他三个选项。
bool noRepeat(int arr[], int o){
for(int i=0; i<3; i++){
if(arr[i] == o)
return false;
}
return true;
}
int main(){
...
//picking correct answer and determining layout
int m = rand()%4, n=0, y, options[3];
if (m == 0) {
while(n<3){
y = rand()%50;
if(noRepeat(options, y) && capitals[y]!=capitals[r])
options[n++] = y;
}
//display according to layout
cout << "\nA: " << capitals[r] << endl;
cout << "B: " << capitals[options[0]] << endl;
cout << "C: " << capitals[options[1]] << endl;
cout << "D: " << capitals[options[2]] << endl;
}
//do the same for the rest
...
}
答案 2 :(得分:0)
就像@Isaiah说的那样,你可以使用while循环测试生成的索引与你的正确答案不一样。
类似的东西:
int index = rand() % 50;
while(index == r)
{
index = rand() % 50;
}
cout << "B: " << capitals[index] << endl;
注意:这仍然可以产生&#34;错误答案&#34;的重复,但我想你得到的重点是避免rand
产生的重复。显然,这是代码只是为了纠正纠正答案的重复,你应该使用rand
,如其他人的评论所述。
答案 3 :(得分:0)
你的问题是选择三个随机大写而不重复。所以这里有一些伪代码。