Hdu1443 & poj1012: Joseph

Source: Internet
Author: User
Problem descriptionthe Joseph's problem is notoriously known. for those who are not familiar with the original problem: From among N people, numbered 1, 2 ,..., n, standing in circle every MTH is going to be executed and only the life
The last remaining person will be saved. joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. for example when n = 6 and M = 5 then the people will be executed in the order
5, 4, 6, 2, 3 and 1 will be saved.

Suppose that there are K good guys and K bad guys. in the circle the first K are good guys and the last K bad guys. you have to determine such minimal m that all the bad guys will be executed before the first good guy.


Inputthe input file consists of separate lines containing K. The last line in the input file contains 0. You can suppose that 0 <k <14.


Outputthe output file will consist of separate lines containing M corresponding to K in the input file.


Sample Input

340
 


Sample output

530
 

 

Q: k Represents k good guys and K bad guys. The first K are good guys and the last K are bad guys. According to the principle of Joseph ring game, the minimum m size when the bad guys are out and the good guys are not out

Train of Thought: due to the small value of K, the table is directly violent enumeration.

# Include <stdio. h> # include <string. h> int main () {int n, a [30], I, j, Min, Jose [14]; for (I = 1; I <14; I ++) {n = 2 * I; memset (A, 0, sizeof (a); min = 1; for (j = 1; j <= I; j ++) {A [J] = (a [J-1] + MIN-1) % (n-J + 1); // (A [J-1] + MIN-1) it calculates the positions of people that can be reached from the current position. (n-J + 1) It is the position of a person after the departure, because the number should start from him, what we get is the location of the next person out of the game. Note that this location is from 0. If (A [J] <I) // the person out of the game, J resets, min added for enumeration {J = 0; min ++ ;}} Jose [I] = min ;}while (~ Scanf ("% d", & N), n) {printf ("% d \ n", Jose [N]);} return 0 ;}

 

 

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.