Joseph's ring (C ++)

Source: Internet
Author: User
Document directory
  • Lab questions (10 questions in total, 1st questions)
Lab questions (10 questions in total, 1st questions)
Title: Joseph Ring
Time limit: 500 MS
Memory limit: 2000 K
Total time limit: 1000 MS
Description: Joseph's ring number is 1, 2, 3 ,......, N people sit around clockwise. You can select a positive integer as the maximum number of messages. The first person starts to report the number in the clockwise direction from 1. When reporting to m, the number of messages stops. The person who reports m is listed, starting from the next person in the clockwise direction to record the number from 1 until all people are listed. The design program outputs the column sequence.
Input: Number of students n report limit M personnel record 1 (Format: Name student ID gender age class health status) personnel record 2...

Personnel Record n

Output: 1st records of personnel in the column: 2nd records of personnel in the column...

Personnel records listed in the n-th report

Input example:
5 3 ami.com 10000001 female 28 Count 43 general slaughter 10000002 male 23 count 79 health Gu Jian 10000003 male 27 count 29 general Yong Fang 10000004 female 20 count 17 health Energy paper ridge 10000005 male october 11 healthy
Output example: Gu Jian, 10000003 male, 27, 29, average
Ami.com 10000001 female 28 = 43
10000005 male, 18, 11 healthy
Slaughter 10000002 male 23 count 79 healthy
Ma Zhongfang, 10000004 female, 20, 17 healthy
Tip: Cyclic table
Source:
View code

 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 struct LNode
6 {
7 int num;
8 char o[20];
9 char y[20];
10 char w[20];
11 char x[20];
12 char e[20];
13 char r[20];
14 LNode *next;
15 };
16
17 typedef LNode *LinkList;
18
19 void CreateCList(LinkList &CList, int N)
20 {
21 int j;
22 LinkList p,q;
23 CList=(LinkList)malloc(sizeof(LNode));
24 p=q=CList;
25 scanf("%s %s %s %s %s %s\n",p->o,p->y,p->w,p->x,p->e,p->r);
26 CList->num=1;
27 for(j=1; j<N; j++)
28 {
29 p=(LinkList)malloc(sizeof(LNode));
30
31 scanf("%s %s %s %s %s %s\n",p->o,p->y,p->w,p->x,p->e,p->r);
32 p->num=j+1;
33 q->next=p;
34 q=p;
35 }
36 p->next=CList;
37 }
38
39 void Run(LinkList CList,int m)
40 {
41 int j;
42 LinkList p,q;
43
44 p=CList;
45 while(p->next!=p)
46 {
47 if(m==1) for(q=p; q->next!=p;q=q->next);
48 for(j=1; j<m; j++)
49 {
50 q=p;
51 p=p->next;
52 }
53 q->next = p->next;
54 printf("%s %s %s %s %s %s\n", p->o,p->y,p->w,p->x,p->e,p->r);
55 free(p);
56 p=q->next;
57 }
58 printf("%s %s %s %s %s %s\n",p->o,p->y,p->w,p->x,p->e,p->r);
59 free(p);
60 }
61
62 int main()
63 {
64 int n,m;
65
66 scanf("%d %d",&n,&m);
67 LinkList CList;
68
69 CreateCList(CList, n);
70
71 Run(CList,m);
72
73 return 0;
74 }

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.