Joseph Ring Problem: There are n individuals in a circle, numbered sequentially. Starting from the 1th person count (from 1-m count), where the people who reported m out of the circle, ask the last left is the original number of that?

Source: Internet
Author: User

Joseph Ring Problem: There are n individuals in a circle, numbered sequentially. Starting from the 1th person count (from 1-m count), where the people who report m out of the circle, ask the last left is the original number of that.
"C Program Design (fourth edition)" P291, the 5th question, I put 1-3 off to the 1-m numbered.

#include <stdio.h>

void Left_num (int* a,int n,int m) {
    int. = 0,count = 0,i = 0;    Out for the number of out, count for numbered, I for the current report of the first few people
    int *p = A;
    int num = 0;
    for (num = 0;num < n;num++) {
        * (a+num) = num+1;
    }   is n personal number 1-n while
    (out < n-1) {
        if (* (p+i)!= 0) {
            count + +;   Not equal to 0 before the Count +.
        }
        if (count = = m) {
            count = 0;
            * (p+i) = 0;
            out++;  Reported m so, the number of out+1, and content set 0, numbered 0
        }
        i++;
        if (i = = N) {
            i = 0;  To the end of the team start
        }
    //output remainder for
    (num = 0; num < n; num++) {
        if (* (A+num)!= 0) {
            printf ("left Num:%d\n ", * (A+num));

}} int main ()
{
    int m,n;
    int a[50] = {0};
    printf ("Please input total num:");
    scanf ("%d", &n);
    printf ("Please input out num:");
    scanf ("%d", &m);   
    Left_num (a,n,m);
    return 0;
}

Output

please input total num:10/input out Num:3 left Num:4 

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.