zoukankan      html  css  js  c++  java
  • 约瑟夫环,指针实现

    约瑟夫环,指针实现

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 
     5 using namespace std;
     6 
     7 typedef struct Node
     8 {
     9     int Id;
    10     struct Node * Next;
    11 } NODE;
    12 
    13 int main()
    14 {
    15 
    16     int n, m;
    17 
    18     scanf("%d %d", &n, &m);
    19 
    20     NODE *head, *p, *q;
    21     head = (NODE *)malloc(sizeof(NODE));
    22 
    23     head->Id = 1;
    24     head->Next = NULL;
    25 
    26     q = head;
    27     for(int i=2; i<=n; i++)
    28     {
    29         p = (NODE*)malloc(sizeof(NODE));
    30         p->Id = i;
    31         p->Next = NULL;
    32         q->Next = p;
    33         q = q->Next;
    34     }
    35     q->Next = head;
    36 
    37     p = head;
    38 
    39     while(p!=p->Next)
    40     {
    41         for(int cnt=1; cnt<=m-1; cnt++)
    42         {
    43             q = p;
    44             p = p->Next;
    45         }
    46         q->Next = p->Next;
    47         //printf("%d ", p->Id);
    48         NODE* t = p;
    49         p = p->Next;
    50         free(t);
    51     }
    52     printf("%d
    ", p->Id);
    53 
    54     return 0;
    55 }
    View Code
  • 相关阅读:
    Transformer详解
    PAT 1012
    PAT 1011
    PAT 1010
    Jordan Lecture Note-3: 梯度投影法
    PAT 1009
    PAT 1008
    Jordan Lecture Note-2: Maximal Margin Classifier
    PAT 1007
    PAT 1006
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/8603753.html
Copyright © 2011-2022 走看看