基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题


N个人坐成一个圆环(编号为1 - N),从第1个人开始报数,数到K的人出列,后面的人重新从1开始报数。问最后剩下的人的编号。
例如:N = 3,K = 2。2号先出列,然后是1号,最后剩下的是3号。
Input
2个数N和K,表示N个人,数到K出列。(2 <= N, K <= 10^6)
Output
最后剩下的人的编号
Input示例
3 2
Output示例
3
相关问题
约瑟夫环
0
约瑟夫环 V2
160
约瑟夫环 V3
1280
result =0 是一个人玩游戏时 剩下的人的下标(0--n-1)
#include <iostream> #include <algorithm> using namespace std ; int main(){ int n , m ; while(cin >> n >> m ){ int result = 0 ; for(int i=2 ; i<=n ; i++){ result = (result + m ) % i ; } result ++ ; if(result <= n){ cout << result << endl ; }else{ cout << result % n << endl ; } } return 0 ; }