C语言 | Leetcode C语言题解之第390题消除游戏
题目:
题解:
int lastRemaining(int n) {int a1 = 1;int k = 0, cnt = n, step = 1;while (cnt > 1) {if (k % 2 == 0) { // 正向a1 = a1 + step;} else { // 反向a1 = (cnt % 2 == 0) ? a1 : a1 + step;}k++;cnt = cnt >> 1;step = step << 1;}return a1;
}