Joseph problem (Detailed solution)

Source: Internet
Author: User

Preface

these days to learn the queue, so try Joseph problem, and then there has been a place did not pass, finally through the point after the passage. The use of the queue has deepened the impression. It is recommended that if the queue's practiced hand can try to solve the problem.

Body

 

Joseph problem (35 points) Bedding: Suppose there are n people in a circle, and then the first person killed the second person, the knife to a third person, the third person killed the fourth person, and so on ...First Note: Difficulty 1: In a circle, we will want to use what data structure? So many people, the first should think of using arrays right, but how to express a circle? The point is, in fact, we can constantly move the front to the back is not the equivalent of a circle? Right? difficulty two: Before a person to kill the last one? The ordinary should want to kill the elements of the person removed, and then the previous element to move back, but this does not reach the effect of the circle into the ring, the focus is to the killing of the people who started to kill, right! It's not wrong, it's killing it, and then putting it on the very end of the array. OK, the difficulty is complete.

Topic content:

Actually Joseph problem.

The hypothesis starts with N people, number 1~n,

Follow the sequence to form a circle in a clockwise fashion.

At the beginning of the game, 1 of the people took the knife.

Then each knife will be spread down to m people,

And the man who finally got the knife will kill his next man,

After the kill, the knife will be handed over to the next person who was murdered.

This is the end of a round.

The game will go through many rounds until only the last person is left.

Example 1:n=5, m=2

First round: Knife to 3, 4 killed, knife to 5 (1 2 3 5)

Second round: Knife to 2, 3 was killed, and the knife to the 5 (1 2 5)

Third round: The Knife to 2, 5 was killed, and the knife to the 1 (1 2)

Round four: The knife was handed to number 1, 2 was murdered, and the last 1 survived.

Example 2:n=4, m=3

First round: Knife to 4, 1 killed, knife to 2 (2 3 4)

Second round: The Knife to 2, 3 was killed, and the knife to the 4 (2 4)

Third round: knife to 2, 4 was killed, and the last 2 survived.

Input format:

Enter the first line as a number T, which represents the measured number of digits.

Then there will be T-pen measurements, each

There will be two numbers n,m, and the numbers will be separated by spaces.

Digital Range:

T < 1000

0 < N <= 1000

0 < M <= 1000

Output format:

Output a line of numbers and add up the number of people who have survived the last one.

Input Sample:

3

5 2

4 3

8 4

Sample output:

4

time limit: 1000ms memory limit: 32000kb  

Let's try to solve it first. It's better to read the article again!

First understand test instructions: Look at the first example: the knife two people after the third person, the fourth person killed. Plus it's a circle, so it's quite a matter of killing two people and moving them to the back, and then it's the front question, right? OK, start writing code.

First we consider the act of killing.

1 int Dequeue () {2     int  z;//Records which person was killed, or if there is no record, how to move it to the back 3     z = ch[ Head]; 4     1)% 1000;//is the head to the back of a person can be, the equivalent of this circle of people without it 5     number_of_items--;//total number minus one  6     return  z; 7 }

Then consider plugging in the back to this action:

1 voidEnqueue (intX) {2     if(Number_of_items = =0) {//If the array is empty, redefine the head and tail of the queue3Tail = Head =0;4ch[0] =x;//and add it in .5}Else {6Tail = (Tail +1) %1000;//the tail point to the back one for storing new people7Ch[tail] =x;8     }9number_of_items++;//Total number plus oneTen}

The next step is the overall design.

scanf ("%d%d",&a,&b);//Enter the total number of people and the number of people to be transmitted
      for(intI=1; I <= A; i++) Enqueue (i);//Put these people in the array. for(intI=1; I <= A-1; i++) {//each round kills one person, altogether kills n-1 individual for(intk=0; K < B%number_of_items; k++) {J=Dequeue (); Enqueue (j); }//every time you move the B man to the back, J .=Dequeue (); Dequeue (); Enqueue (j);//As the Mat says, kill two people and put the first person behind the array. }

Attention:

1. When adding an array element, be careful to consider when the array is empty.

2.

 for (int k=0; k < b%number_of_items; k++) {                = Dequeue ();                Enqueue (j);            }

Note that this code, is this place card for a long time, see No, when B is very big time, run for very long, there are already 3 for loop, so the idea tries to reduce the complexity of the algorithm. It was found that B could be optimized , which is the key point.

3. Each time the array element is added, the total number of people remember to add one, delete array elements, the total number of people remember to subtract one.

OK, then write the total code:

#include <stdio.h>intch[ +];intHead,tail,number_of_items;voidEnqueue (intx) {if(Number_of_items = =0) {Tail= Head =0; ch[0] =x; } Else{Tail= (Tail +1) % +; Ch[tail]=x; } number_of_items++;} intDequeue () {intZ; Z=Ch[head]; Head= (Head +1) % +; Number_of_items--; returnZ;} intMain () {intn,j,a,b; intanswer; intsum =0; scanf ("%d",&N);  for(intI=0; I < n; i++) {scanf ("%d%d",&a,&b); ch[ +] =0;//Pay attention to each update of the array Number_of_items=0;//update on the total number of people Tail= Head =0;  for(intI=1; I <= A; i++) Enqueue (i);  for(intI=1; I <= A-1; i++) {             for(intk=0; K < B%number_of_items; k++) {J=Dequeue ();            Enqueue (j); } J=Dequeue ();            Dequeue ();        Enqueue (j); } Sum+=Ch[head]; } printf ("%d", sum); return 0;}

Note: Each time the array is exhausted, the update assignment is 0, and the total number is updated.

Summary

 The problem of the card is to reduce the complexity of the algorithm, some of the variables are optimized. I also through others to understand the optimization here, but also to learn a lot.

2016-03-06 14:14:02

Joseph problem (Detailed solution)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.