-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path约瑟夫环问题.cpp
49 lines (45 loc) · 862 Bytes
/
约瑟夫环问题.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// Created by 不死鸟Anka on 2019-07-29.
//
//#include "约瑟夫环问题.h"
#include <iostream>
using namespace std;
struct node {
long d;
node *next;
};
long n, m;
node *head, *p, *r;
int cir(int n, int m) {
int p = 0;
for (int i = 2; i <= n; i++) {
p = (p + m) % i;
}
return p + 1;
}
int main() {
long i, j, k, l;
cin >> n >> m;
head = new node;
head->d = 1;
head->next = NULL;
r = head;
for (i = 2; i <= n; i++) {
p = new node;
p->d = i;
p->next = NULL;
r->next = p;
r = p;
}
r->next = head;
r = head;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m - 2; j++) r = r->next;
cout << r->next->d << " ";
r->next = r->next->next;
r = r->next;
}
long a, b;
cin >> a >> b;
cout << cir(a, b);
}