C Language Programming: There are n individuals in a circle, the order automatic arranging. Start off from the first person ... __ programming

Source: Internet
Author: User

Title: There are n individuals, numbered from 1 to n, enclosed in a circle in numbered order. Starting from the first person (1 report 3), where 3 of the people who reported out of the circle.
Q: What is the number of the last person left?
==============================
This is Baidu know, very common topic, most have the correct answer.
Such as:
Http://zhidao.baidu.com/question/95065794.html
Http://zhidao.baidu.com/question/211023828.html
Http://zhidao.baidu.com/question/341545042.html
Http://zhidao.baidu.com/question/353160064.html
......

But the process of looking at these answers is too troublesome for the majority.
To do and to talk about this problem, write a program, the feeling of self is the shortest. Oh, welcome users to shoot bricks.
The procedure is as follows:
==============================
#include <stdio.h>
#define N 5//number
void Main ()
{
int A[n] = {0}, i = 0, Call_n = 0, out_n = 0;

while (1) {//Cycle count
if (a[i] = = 0) {//If you are alive
if (Out_n = = (N-1)) break; If there's only one left.
call_n++; Count
Call_n%= 3; The maximum is 3, and by 3 it starts at 0.
if (Call_n = = 0) {A[i] = 1; out_n++}//is 0 (that is 3) out
}
i++;      I%= N; The loop turns to the next person
}
printf ("Last remaining number is:%d\n", i + 1);
}//------------------finish.
==============================

In some topics, it is not difficult to use pointers, but the pointers and arrays are uniform, and the program is as follows:

==============================
#include <stdio.h>
#define N 5//number
void Main ()
{
int A[n] = {0}, i = 0, out_n = 0, Call_n = 0, *p;
p = A;
while (1) {//Cycle count
if (*p = 0) {//If you are alive
if (Out_n = = (N-1)) break; If there's only one left.
call_n++; Count
Call_n%= 3; The maximum is 3, and by 3 it starts at 0.
if (Call_n = = 0) {*p = 1; out_n++}//= 0 (that is, 3) out
}
p++;  if (p = = A + N) p = A; The loop turns to the next person
}
printf ("The last remaining person's number is:%d\n", p + 1-a);
}//------------------finish.
==============================

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.