约瑟夫环
终于过了(在没看类似题目的情况下),心情激动,分享答案,仅供参考。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; bool life[101]; int main() { memset(life, true, sizeof(life)); int n, k, m, ans, np = 0; cin >> n >> k >> m; ans = k; while (np < n-1) { for (int i=1; i<m; i++) { while (life[ans] == false) { if (ans < n-1) { ans++; }else{ ans = 0; } } if (ans < n-1) { ans++; }else{ ans = 0; } while (life[ans] == false) { if (ans < n-1) { ans++; }else{ ans = 0; } } } life[ans] = false; if (ans < n-1) { ans++; }else{ ans = 0; } np++; } for (int ans=0; ans<n; ans++) { if (life[ans] == true) { cout << ans << endl; break; } } return 0; }