C language--n Individual in a circle to eliminate the problem

Source: Internet
Author: User

"One" Problem description:

There are 17 people in a circle (numbered 0~16), starting from the No. 0 number of people from 1,

Those who report a multiple of 3 leave the circle and count until the last one is left.

Ask this person what is the original location?

"II" Problem solving:

1. Define the status of each number in the array record (whether it is eliminated);

2. If the number of persons who have not been eliminated is counted to 3, if the person is eliminated;

3. Continue this process until all are eliminated;

"Three" Sample code:

1 /*2 16. There are 17 people in a circle (numbered 0~16), starting from the No. 0 number of people from 1,3 Those who report a multiple of 3 leave the circle and count until the last one is left. 4 ask this person what is the original location? 5 */6 7 #define_crt_secure_no_warnings8#include <stdio.h>9#include <stdlib.h>Ten  One voidMain () A { -     intTotal = -, need = -, k =3; -     intindex =0, Count =0, i =0; the     inta[ -] = {0 }; -      for (;;) -     { -Index = index% total +1; +         if(A[index] = =0) -         { +i = (i +1) %K; A             if(i = =0) at             { -count++; -A[index] =1; -             } -         } -         if(count = = need) Break; in     } -printf"index =%d\n", index); toSystem"Pause"); +}
View Code

C language--n Individual in a circle to eliminate the problem

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.