Joseph PHUs problem beginner (using arrays)

Source: Internet
Author: User

Problem description:

This is a classic problem. For example, if there are 6 people, the first person starts to report data from 1, and the numbers reported by the next person increase in sequence, when the reported number is a certain number, the reported person goes out and the game continues. The number of people who have not been out of the game continues from 1 until all the people have been out of the game. Obtain the order of exit.

For example, there are 6 people, respectively 1, 2, 3, 4, 5, 6. If the number of people reported to 3 goes out, the order of departure should be: 3, 6, 4, 2, 5, 1

Solution:

You can set the flag bit of the array to 0 to indicate that the array is out. When it is traversed, the count does not need to be increased. It is also a common idea to set the flag position in actual development.

Code:

#include <stdio.h>/*total people number*/#define ALL 100/*people leave when count to left_counter*/#define left_counter 3/*people array*/int people[ALL];/*init people array*/void initPeople(){int i = 0 ;for (i = 0; i < ALL; i++){people[i] = i+1;}}/*print people array*/void printPeople(){int i = 0;for (i = 0; i < ALL; i++){printf("%d ",people[i]);}printf("\n");}int main(void){initPeople();int left = ALL;/*init total left number*/int counter = 0;/*init counter*/int i = 0;/*init array index*/while (1){if (0 != people[i]){counter++;}/*if counter == left_counter , peopler out, set people[i] = 0   counter = 0;   left--;**/if (counter == left_counter){left--;printf("%d is out\n", people[i]);counter = 0;people[i] = 0;printPeople();}/*if no people left, problem solved*/if (0 == left){printf("problem finished!\n");break;}/*increase index*/i++;if (i == ALL){i = 0;}}return 0;}

Joseph PHUs problem beginner (using arrays)

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.