Joseph Ring Question

Source: Internet
Author: User

There are many online sayings. Here is a brief description.

A group of people in a circle, everyone has their own key. After the dequeue with its key to do the next loop out of the team number.

Source

#include <stdio.h> #include <stdlib.h>typedef struct _node{int ord;//order int key;//keystruct _node* next;} Node,*pnode;pnode Create () {int K,o=1;pnode head,cur,h;if (scanf ("%d", &k), k) {head=h=cur= (pnode) malloc (sizeof ( Node)); h->ord=o++;h->key=k;h->next=null;} Else{return NULL;} while (scanf ("%d", &k), k) {cur= (pnode) malloc (sizeof (Node)); cur->ord=o++;cur->key=k;cur->next=null;h- >next=cur;h=cur;} H->next=head;return Head;} void Joh (Pnode head,int start,int m) {pnode h=head,tem;int count=1;while (h->next->ord!=start) {h=h->next;} while (H->next) {while (count<m) {h=h->next;++count;} if (count==m) {m=h->next->key;  printf ("ord=%d,key=%d\n",h->next->ord,h->next->  Key); For testing with Tem=h->next;h->next=h->next->next;free (TEM); count=1;} if (h==h->next) {printf ("ord=%d,key=%d\n", H->next->ord,h->next->key); free (h); break;}}} void Show (Pnode head) {pnode h=head;while (h) {printf ("ord=%d,key=%d\n", H->ord,h->key); H=h->next;if (H==head) {break;}}} int main () {Pnode head=create (); Show (head);p rintf ("\ n"); Joh (head,3,3); return 0;}

Test data

ord=1,key=2
Ord=2,key=3
Ord=3,key=4
ord=4,key=2
Ord=5,key=5
Ord=6,key=3
ord=7,key=2
Ord=8,key=4
Ord=9,key=3
Ord=10,key=4
ord=11,key=2
Ord=12,key=3
ord=13,key=2


Ord=5,key=5
Ord=10,key=4
ord=1,key=2
Ord=3,key=4
Ord=8,key=4
ord=13,key=2
ord=4,key=2
ord=7,key=2
ord=11,key=2
Ord=2,key=3
Ord=12,key=3
Ord=6,key=3
Ord=9,key=3



Joseph Ring Question

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.