Suitors In an ancient land, the beautiful princess Eve had many suitors. She dec
ID: 3676374 • Letter: S
Question
Suitors In an ancient land, the beautiful princess Eve had many suitors. She decided on the following procedure to determine which suitor she would marry. First, all of the suitors would be lined up one after the other and assigned numbers. The first suitor would be number 1, the second number 2, and so on up to the last suitor, number n. Starting at the first suitor she would then count three suitors down the line (because of the three letters in her name) and the third suitor would be eliminated from winning her hand and removed from the line. Eve would then continue, counting three more suitors, and eliminating every third suitor. When she reached the end of the line she would continue counting from the beginning. For example, if there were 6 suitors then the elimination process would proceed as follows: 123456 initial list of suitors, start counting from 1 12456 suitor 3 eliminated, continue counting from 4 1245 suitor 6 eliminated, continue counting from 1 125 suitor 4 eliminated, continue counting from 5 15 suitor 2 eliminated, continue counting from 5 1 suitor 5 eliminated, 1 is the lucky winner Write a program that uses a vector to determine which position you should stand in to marry the princess if there are n suitors. You will find the following method from the Vector class useful: v.erase(iter); // Removes element at position iter For example, to use this method to erase the 4th element from the beginning of a vector variable named theVector use: theVector.erase(theVector.begin() + 3); The number 3 is used since the first element in the vector is at index position 0. Allow your program run continuously as often as the user wishes. Finally, your name as the programmer who wrote the program should be displayed on screen as well at the end. Note: Comments at the top of your source file must include a brief description, your name and the date of finishing. The number of suitors can’t be negative. The program can’t be crashed at run time by any reasons. CodeMate Hint: Use a vector of size n and a loop that continues to eliminate the next suitor until the size of the vector includes only one element
Explanation / Answer
I hope this one will help you out if not let me know will provide you another code.
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
int n;
vector<int> vec;
cout << "Enter the number of suitors: " << endl;
cin >> n;
for (int i = 2; vec.size() > 1; i = (i + 2) % vec.size())
{
vec.erase(vec.begin() + i);
for (int j = 0; j < vec.size(); j++)
cout << vec[j];
cout << endl;
}
vec.erase(vec.begin()+2);
for(unsigned i = 0; i <= vec.size(); i++){
cout << vec[i] << endl;
}
}
Else tr this one...
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> l;
int nu;
int in;
cout << "Enter the number of suitors: ";
cin >> nu;
for (in=0; in<n; in++) l.push_back(in);
int delta = 3;
// want to remove every third
in = delta;
list<int>::iterator it = l.begin();
while (l.size() > 1)
{
// loop until only one left
if (--in == 0)
{
// is it third ?
list<int>::iterator it2 = it++;
// move pointer one step further (erase would destroy it)
l.erase(it2);
// remove it
in = delta;
// rearm counter
}
else it++;
// was not third, simply goes on
if (it == l.end()) it = l.begin();
// if at end, go back to begin
}
cout << "Winner is number : " << l.front() + 1 << endl; // add one as our list was 0,1,...n-1
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.