JosephRing.h
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include<stdio.h>
#include<stdlib.h>
typedef struct Jew{
int num;
struct Jew* next;}Jew;
Jew* initlink(int n);
void findpity(Jew* head, int ks, int m);
JosephRing.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "JosephR.h"
Jew* initlink(int n){
Jew* head = new Jew;
head->num = 1;
head->next = NULL;
Jew* circul = head;
for (int i = 2; i < n + 1; i++) { Jew* nextone = new Jew;
nextone->num = i;
nextone->next = NULL;
circul->next = nextone; }
circul->next =head;to
return head;}
void findpity(Jew* head, int ks, int m){ Jew* tail = head;
printf("tail now:%d", tail->num); while (tail->next != head) { tail = tail->next;
printf("tail is:%d\n", tail->num);
}
Jew* pityone = head;
while (pityone->num != ks) { tail = pityone;
pityone = pityone->next; }
while (pityone->next!=pityone) { for (int i = 1; i < m; i++) { tail = pityone; pityone = pityone->next; } printf("pityone is No.%d\n", pityone->num); tail->next = pityone->next;//pityone died
pityone = tail->next; } free(pityone);}
main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include"JosephR.h"
int main() {
printf("input all num n:,dead num m:,start from ks: \n");
int n, m, ks;
scanf("%d%d%d", &n, &m, &ks);
Jew* head = initlink(n); findpity(head, ks, m);
return 0;}