1 answer

The Josephus Problem People are standing in a circle (Links to an external site.)Links to an external site. waiting to

Question:

The Josephus Problem

People are standing in a circle (Links to an external site.)Links to an external site. waiting to be executed. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a specified number of people are skipped, the next person is executed. The procedure is repeated with the remaining people, starting with the next person, going in the same direction and skipping the same number of people, until only a specified number of persons remain, and are freed. The problem — given the number of people, starting point, direction, and number to be skipped — is to choose the positions in the initial circle to avoid execution. In the original Josephus problem 41 men stand in a circle and decide to kill every 3rd person until 2 men remain who then kill themselves. Write a function that declares a local queue to solve the original Josephus problem as a special case. The function's prototype is vector solveJosephus( int ncandidates, int nsurvivors);

The function returns the positions in the original circle of the survivors. The function returns an empty vector if any of its parameters is bad. What are the characteristics of a bad parameter?

Data Structure C++. Visual Studios 2017


Answers

#include <bits/stdc++.h>
using namespace std;

/* structure for a Node */
struct Node {
    int data;
    Node* next;
    Node(int x)
    {
        data = x;
        next = NULL;
    }
};

/*Utility function to print the circular linked list*/
void printList(Node* head)
{

    if (head == NULL)
        return;
    Node* temp = head;
    do {
        cout << temp->data << "->";
        temp = temp->next;
    } while (temp != head);
    cout << head->data << endl;
}

/*Function to delete every kth Node*/
void deleteK(Node** head_ref, int k)
{
    Node* head = *head_ref;

    // If list is empty, simply return.
    if (head == NULL)
        return;

    // take two pointers - current and previous
    Node *curr = head, *prev;
    while (true) {

        // Check if Node is the only Node\
        // If yes, we reached the goal, therefore
        // return.
        if (curr->next == head && curr == head)
            break;

        // Print intermediate list.
        printList(head);

        // If more than one Node present in the list,
        // Make previous pointer point to current
        // Iterate current pointer k times,
        // i.e. current Node is to be deleted.
        for (int i = 0; i < k; i++) {
            prev = curr;
            curr = curr->next;
        }

        // If Node to be deleted is head
        if (curr == head) {
            prev = head;
            while (prev->next != head)
                prev = prev->next;
            head = curr->next;
            prev->next = head;
            *head_ref = head;
            free(curr);
        }

        // If Node to be deleted is last Node.
        else if (curr->next == head) {
            prev->next = head;
            free(curr);
        }
        else {
            prev->next = curr->next;
            free(curr);
        }
    }
}

/* Function to insert a Node at the end of
a Circular linked list */
void insertNode(Node** head_ref, int x)
{
    // Create a new Node
    Node* head = *head_ref;
    Node* temp = new Node(x);

    // if the list is empty, make the new Node head
    // Also, it will point to itself.
    if (head == NULL) {
        temp->next = temp;
        *head_ref = temp;
    }

    // traverse the list to reach the last Node
    // and insert the Node
    else {
        Node* temp1 = head;
        while (temp1->next != head)
            temp1 = temp1->next;
        temp1->next = temp;
        temp->next = head;
    }
}

/* Driver program to test above functions */
int main()
{
    int i;
    // insert Nodes in the circular linked list
    struct Node* head = NULL;
    for(i=1;i<=41;i++)
    {
   insertNode(&head, i);
    }


    int k = 2;

    // Delete every kth Node from the
    // circular linked list.
    deleteK(&head, k);

    return 0;
}

.

Similar Solved Questions

1 answer
If there are 50 vehicles on a 4000-ft highway lane and the average time headway between...
If there are 50 vehicles on a 4000-ft highway lane and the average time headway between two adjacent vehicles is 2.2 seconds. 3. Calculate traffic density in vehicle/mile (vpm) and average space headway in feet/vehicle (fpv) (1pt) 1) 2) Calculate traffic volume in vehicle/hour (vph). (1pt) 3) Calcul...
1 answer
Question 3 of 3 (2 points) 6.2 Section Exercise 20 If the average price of a...
Question 3 of 3 (2 points) 6.2 Section Exercise 20 If the average price of a new one family home is $246.300 with a standard deviation of $15,000, find the minimum and maximum prices of the houses that a contractor will build to satisfy the middle 40% of the market. Assume that the variable is norma...
1 answer
What is the product of the following reaction? 1.) KMnO4 cold dilute 2.) NaOH O OH...
what is the product of the following reaction? 1.) KMnO4 cold dilute 2.) NaOH O OH + IO||III. H H OH OH enantiomer enantiomer A B D...
1 answer
Question 7 2.38 Points Solve the equation 2x+64=146. Calculate your answer to the nearest whole number....
Question 7 2.38 Points Solve the equation 2x+64=146. Calculate your answer to the nearest whole number. Do not put the x= in your answer. Only put the number value for your answer. Add your answer Question 8 2.38 Points Four teams will receive prize money based on their ticket sales. The total prize...
1 answer
Part A of the question is answered, what is the answer to Part B? Constants Part...
Part A of the question is answered, what is the answer to Part B? Constants Part A In a hot water heater, water warms when electric potential energy is converted into thermal energy. Determine the energy needed to warm 190 kg of water by 13° C. The specific heat of water is 4180 J/kg-C" Expr...
1 answer
Physical Chemistry, Please show work so I can understand, Thank You! Chemistry 452 2016-10-10 Glucose-Lactate Under...
Physical Chemistry, Please show work so I can understand, Thank You! Chemistry 452 2016-10-10 Glucose-Lactate Under anaarobic conditions glucose is broken down in the muscle tissue to form lactic acid according to the reaction Part D GGH1206 -2CHзсH (он соо&...
1 answer
€ Co wb /Studentment Responsep234664178012 12. - POINTS SERCP115.6.P.040. MY NOTES ASK YOUR TEACHER r n...
€ Co wb /Studentment Responsep234664178012 12. - POINTS SERCP115.6.P.040. MY NOTES ASK YOUR TEACHER r n between Nock and (a) A block with a mamis pulled along a hormonal surface for a distance by a constant force at an angle with respect to the hotel. The conto twists the force d by consulto no...
1 answer
Equivalent Units of Production The Converting Department of Hopkinsville Company had 600 units in work in...
Equivalent Units of Production The Converting Department of Hopkinsville Company had 600 units in work in process at the beginning of the period, which were 60% complete. During the period, 12,400 units were completed and transferred to the Packing Department. There were 680 units in process at the ...
1 answer
Marcelino Co.'s March 31 inventory of raw materials is $85,000. Raw materials purchases in April are...
Marcelino Co.'s March 31 inventory of raw materials is $85,000. Raw materials purchases in April are $530,000, and factory payroll cost in April is $385,000. Overhead costs incurred in April are: indirect materials, $58,000; indirect labor, $28,000; factory rent, $30,000; factory utilities, $20,...
1 answer
2. The rate constants for the reaction CHCI3 (g)+CI (g) CHCI2 (g)+Cl2 (g) at different temperatures...
2. The rate constants for the reaction CHCI3 (g)+CI (g) CHCI2 (g)+Cl2 (g) at different temperatures are tabulated below k (107 M1 s1) T (K) 357 400 1.72 2.53 3.82 458 5.20 524 5.61 7.65 533 615 a. Construct an Arrhenius plot of the tabulated data as presented above b. From the plot, calculate the va...
1 answer
Two charges, Q1= 2.70 μC, and Q2= 5.90 μC are located at points (0,-3.00 cm )...
Two charges, Q1= 2.70 μC, and Q2= 5.90 μC are located at points (0,-3.00 cm ) and (0,+3.00 cm), as shown in the figure. What is the magnitude of the electric field at point P, located at (5.50 cm, 0), due to Q1 alone? 6.18×106 N/C You are correct. Previous Tries What is the x-...
1 answer
Describe 2 (two) methods that will be useful in enhancing data quality? Would internal validity and...
Describe 2 (two) methods that will be useful in enhancing data quality? Would internal validity and reliability be correct for this question or would you recommend something else?...
1 answer
Q5 Q6 Q7 Q8 thx Sloane, Inc., manufactures and sells snowboards. Sloane manufactures a single model,...
Q5 Q6 Q7 Q8 thx Sloane, Inc., manufactures and sells snowboards. Sloane manufactures a single model, the Pipex. In the summer of 2017, Sloane's management accountant gathered the following data to prepare budgets for 2018: Materials and Labour Requirements Direct materials Wood Fiberglass Di...
1 answer
HOW FAST DOES IT TAKE FOR RUBBER TO to reach its melting point.
HOW FAST DOES IT TAKE FOR RUBBER TO to reach its melting point....
1 answer
Question 32 (10 points) A bicycle rider rides away from home along a highway and back...
Question 32 (10 points) A bicycle rider rides away from home along a highway and back along the same road in such a way that her distance from home at time t is given by xit)- t-8Equation:eqn t® ,height-32.width-24.size-14.bgcolor-#FFFFFF,title-eqn-2,controls-false}+ 16 Equation:eqn- t2 ,height-...
1 answer
18 Task analysis examines the components of a specific job. the description of what constitutes successfully...
18 Task analysis examines the components of a specific job. the description of what constitutes successfully completing a task. a particular process and its component tasks mission statement and purpose of an organization. 36. Job analysis examines the description of what constitutes successfully co...
1 answer
41. Sex is based upon ________ characteristics that distinguish males from females, while gender refers to...
41. Sex is based upon ________ characteristics that distinguish males from females, while gender refers to a(n) ________ characteristic...