Question

In: Computer Science

Problem Statement Josephus attended a party where they had a rather strange raffle. Everyone was to...

Problem Statement

Josephus attended a party where they had a rather strange raffle. Everyone was to stand in a circle, and every Mth person was to be eliminated until only one person was remaining, who won the lawnmower.

Write the function Josephus, which takes as parameters a vector<string> representing the N guests at the party in the order they line up for the raffle, and the integer M, giving which person will be eliminated at each turn. Your function should return a string giving the name of the winner of the raffle (the last person standing).

Hint: While there is a mathematical solution, a queue will be much more straightforward.

Constraints

  • N, the length of the vector<string> guests, will be greater than 0 and less than 2,000.
  • M will be greater than zero and less than 2,000.

Examples

  1. guests = {"Josephus", "Titus", "Simon", "Eleazar"}
    M = 3 
    Returns: "Josephus"

    Counting every third person from Josephus (remember, they are in a circle), Simon is eliminated first. Skipping Eleazar and Josephus, Titus goes next. Skipping Eleazar and Josephus, we come back to Eleazar. Josephus, who cleverly suggested M = 3, comes out the winner!

  2. guests = {"Bradford", "Rosette", "Ji", "Ashleigh", "Avery", "Lavonna", "Fredericka"} 
    M = 2 
    Returns: "Fredericka"
  3. guests = {"Tiffany", "Randy", "Kit", "Sharlene", "Marquerite", "Debra", "Pok", "Tanisha", "Claudio"} 
    M = 17 
    Result: "Debra"

Given Function

string Josephus(vector<string> guests, int M) {
// your code here
}

Use the given function to solve the problem statement. Please follow the given notes and constraints. Please code this problem in C++.

Solutions

Expert Solution

#include <iostream>
#include <vector>
using namespace std;
string Josephus(vector<string> guests, int M) {
int startPosition = 0; /*to hold the value to know where to start counting initally it is 0*/
while(guests.size()>1){
int nextElemToDelete = startPosition;
nextElemToDelete +=(M);
nextElemToDelete%=guests.size(); //circular counting after last element start from zero
nextElemToDelete = (nextElemToDelete-1); //index of vector starts at 0 so we have to substract 1.
if(nextElemToDelete==-1){//if nextElemToDelete ==-1 we have to delete last element
nextElemToDelete = guests.size()-1; // setting nextElemToDelete last element
}
guests.erase(guests.begin()+nextElemToDelete);
startPosition = nextElemToDelete;
}
return guests[0];
}
int main()
{
vector<string> guests;
//guests = {"Josephus", "Titus", "Simon", "Eleazar"};
guests = {"Tiffany", "Randy", "Kit", "Sharlene", "Marquerite", "Debra", "Pok", "Tanisha", "Claudio"} ;
//guests = {"Bradford", "Rosette", "Ji", "Ashleigh", "Avery", "Lavonna", "Fredericka"};
string a = Josephus(guests,17);
cout<<a;

return 0;
}


Related Solutions

JAVA for Dummies HELP Problem Statement: Jeff Bezos has decided to have a party at his...
JAVA for Dummies HELP Problem Statement: Jeff Bezos has decided to have a party at his mansion, and he’s invited 50 couples. As the couples arrive at the mansion, they receive a ticket with a number (like at the deli counter), with the first couple receiving the tickets labeled 1 and 2, the second couple getting 3 and 4, and so on. Jeff has noticed in the past that when he throws parties, conversation groups most often occur in groups...
Problem 3 (25 marks) Eagles Inc. had the following statement of financial position at the end...
Problem 3 Eagles Inc. had the following statement of financial position at the end of operations for 2017: Cash 32000 Accounts payable 48000 Accounts receivable 33920 Bonds payable 65600 FV-NI investments 51200 Common shares 160000 Equipment (net) 129600 Retained earnings 37120 Land 64000 Total 310720 310720 During 2018, the following occurred: 1. Eagles sold its FV-NI investments portfolio at a gain of $9,600. 2. A parcel of land was purchased for $75,200. 3. An additional $60,800 worth of common shares...
Please write simple python program with explanation Problem statement: Stock markets are venues where buyers and...
Please write simple python program with explanation Problem statement: Stock markets are venues where buyers and sellers of shares meet and decide on a price to trade.Eddard is very interested in trading in stock market and he started gathering information of one stock.He has projected market prices for that share over the next n months.Eddard wants to purchase and resell the share to earn profits.He is much worried about the loss that might occur so he wanted to calculate "Minimum...
Additional Problem 12 Sheridan Ltd., which follows ASPE, had the following comparative statement of financial position:...
Additional Problem 12 Sheridan Ltd., which follows ASPE, had the following comparative statement of financial position: Sheridan Ltd. Comparative Statement of Financial Position As at December 31 Assets 2018 2017 Cash $ 70,520 $ 43,000 Accounts receivable 116,960 87,720 Inventories 68,800 103,200 Prepaid insurance 8,600 6,880 Equipment 264,880 223,600 Accumulated depreciation-equipment (60,200 ) (43,000 ) Patents 68,800 86,000 Total assets $ 538,360 $ 507,400 Liabilities and Shareholders’ Equity Accounts payable $ 79,120 $ 68,800 Interest payable 6,880 10,320 Wages payable...
Additional Problem 12 Sheridan Ltd., which follows ASPE, had the following comparative statement of financial position:...
Additional Problem 12 Sheridan Ltd., which follows ASPE, had the following comparative statement of financial position: Sheridan Ltd. Comparative Statement of Financial Position As at December 31 Assets 2018 2017 Cash $ 70,520 $ 43,000 Accounts receivable 116,960 87,720 Inventories 68,800 103,200 Prepaid insurance 8,600 6,880 Equipment 264,880 223,600 Accumulated depreciation-equipment (60,200 ) (43,000 ) Patents 68,800 86,000 Total assets $ 538,360 $ 507,400 Liabilities and Shareholders’ Equity Accounts payable $ 79,120 $ 68,800 Interest payable 6,880 10,320 Wages payable...
12.  Problem 3.12 (Statement of Cash Flows) eBook Hampton Industries had $70,000 in cash at year-end 2018...
12.  Problem 3.12 (Statement of Cash Flows) eBook Hampton Industries had $70,000 in cash at year-end 2018 and $28,000 in cash at year-end 2019. The firm invested in property, plant, and equipment totaling $300,000 — the majority having a useful life greater than 20 years and falling under the alternative depreciation system. Cash flow from financing activities totaled +$210,000. Round your answers to the nearest dollar, if necessary. What was the cash flow from operating activities? Cash outflow, if any, should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT