"title" The host prepares to pick a lucky audience from N students, because everyone wants to be a lucky audience, the teacher has to take such a way: all the students stand in a row, from front to back in order to count off. 1,2,1,2 ... The odd students quit the team, the rest of the students move forward, and then re-1,2,1,2 after the trip to ... Count off, the singular to quit the team, so the last one left a lucky audience. Programming to find the lucky audience, in the original queue where to stand? (n by keyboard input, n<255)
"Get Started" first you can lay a draft: Here are the assumption that there are 6 students (out of 0 instead of students) at the beginning of the position: [1] [2] [3] [4] [5] [6] First round count: 1 2 1 2 1 2 first round screening: 0 [2] 0 [4] 0 [6] second round count: 0 1 0 2 0 1 second round screening: 0 0 0 [4] 0 0--------------------------- ----------------------------------------At first my idea is to fill the number of non-0 to 0, the back of the thought can be directly extracted all non-0th code directly instead of the original array. The code is as follows:
-
-
1 Span style= "color: #0000ff;" >for i:=1 to n do 2 if A[i]<>0 then 3 begin 4 inc (j); 5 b[j]:=a[i]; 6 end ;
This is really a method but I personally feel a little cumbersome, then we take a closer look at the previous draft, is not found what? Yes, we can call the DIV directly to re-count:
for i:=1to dodiv2;
Is it the enlightened?
The Joseph question in Pascal's language: the Lucky audience