The Joseph question in Pascal's language: the Lucky audience

Source: Internet
Author: User

"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.  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:
    1.  for i:=1to dodiv2;
Is it the enlightened?

The Joseph question in Pascal's language: the Lucky audience

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.