约瑟夫环问题。
思路:一个一个杀掉,再逆向补回来还原位置。
参考解释
int lastRemaining(int n, int m){
int pos = 0;
for(int i = 2; i <= n; i++){
pos = (pos + m) % i;
}
return pos;
}
约瑟夫环问题。
思路:一个一个杀掉,再逆向补回来还原位置。
参考解释
int lastRemaining(int n, int m){
int pos = 0;
for(int i = 2; i <= n; i++){
pos = (pos + m) % i;
}
return pos;
}