The children's game (the last remaining number in the circle) linked list

Source: Internet
Author: User

Children's games (the last remaining number in the circle)
    • Number of participants: 604 time limit: 1 seconds space limit: 32768K
    • by scale: 27.00%
    • best record: 0 ms|0k (from    water)
The title describes the annual children's Day, Nowcoder will prepare some small gifts to visit the orphanage children, this year is also the case. HF as the senior veteran of the Nowcoder, naturally also prepared some small games. Among them, there is a game like this: first, let the children surround a big circle. Then, he randomly assigned a number m, so that the number of children numbered 0 began to count. Every time the child shouted to M to sing a song, and then can be in the Gift box arbitrary selection of gifts, and no longer back to the circle, from his next child, continue to 0...m-1 count off .... Go on like this .... Until the last child left, can not perform, and get Nowcoder valuable "Detective Conan" Collector's Edition (limited number of seats Oh!!) ^_^). Please try to think, which one of the children will get the present?

Topic Link:http://www.nowcoder.com/practice/f78a359491e64a50bce2d89cff857eb6?rp=3&ru=/ta/coding-interviews& Qru=/ta/coding-interviews/question-ranking

Combine test instructions: is n person from 0 serial number to n-1, then surround in a circle, give a M individual exit circle, then later, this is the simulation linked list Delete node, the last remaining is the target node;

The topic is not difficult, attention to detail, such as m<=0;n<=0, these are to own consideration, even if the topic did not say;

I have done a more technical topic before: Http://www.cnblogs.com/yuyixingkong/p/3254566.html interested can look at;

#include <stdio.h> #include <stdlib.h>typedef struct list {int val;struct list *next;  List (int x): Val (x), Next (NULL) {}}list;class solution {Public:int lastremaining_solution (unsigned int n, unsigned int m) {if (n==0| |        m==0) return-1;        if (m==1) return n-1;        List *l,*p;        L=creatlist (L,n);        int k=0;            while (L) {//printf ("k=%d\n", K);            k++;  if (k%m==0) {p->next=l->next;                printf ("-----%d\n", l->val);                Free (L);                l=p->next;            k=1;            } p=l;            l=l->next;        if (p==l) return p->val;        }} list* creatlist (List *phead,int N) {if (n==0) return NULL;        List *p,*q;        Phead=new List (NULL);        P=phead;        int xx=0;        p->val=xx;            while (--n) {q=new List (++xx);            p->next=q;        p=q;  }      p->next=phead;    return phead;    }};int Main () {solution so;    int n,m;    scanf ("%d%d", &n,&m); int Ans=so.    Lastremaining_solution (N,M);    printf ("%d\n", ans); return 0;} /* Test Case: 0,0 the corresponding output should be: -1*/


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The children's game (the last remaining number in the circle) linked list

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.