1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #include <stdio.h>
- #include <stdlib.h>
- int lastRemaining(int n, int m) {
- int *people = (int *)malloc(n * sizeof(int));
- int i;
- for (i = 0; i < n; i++) {
- people[i] = 1;
- }
-
- int remaining = n;
- int count = 0;
- int index = 0;
- for (i = 0; remaining > 1; i = (i + 1) % n) {
- if (people[index] == 1) {
- count++;
- if (count == m) {
- people[index] = 0;
- count = 0;
- remaining--;
- }
- }
- index = (index + 1) % n;
- }
-
- int result = 0;
- for (i = 0; i < n; i++) {
- if (people[i] == 1) {
- result = i + 1;
- break;
- }
- }
-
- free(people);
- return result;
- }
- int main() {
- int n, m;
- scanf("%d %d", &n, &m);
-
- int result = lastRemaining(n, m);
- printf("%d", result);
-
- return 0;
- }
|