#include <iostream> #include <vector> #include <math.h> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; int whoLeft(unsigned int num); int getBits(unsigned int num); int whoLeft2(unsigned int num); int main() { srand((unsigned)time(NULL)); for(int i=0; i<10; i++) { int num = rand()%1000; cout << whoLeft(num) << endl; cout << whoLeft2(num) << endl; cout << endl; } } int whoLeft2(unsigned int num) { // 取得num二进制有几位 int bits = getBits(num+1); return pow(2, bits-1)-1; } int whoLeft(unsigned int num) { // 初始化状态数组 vector<bool> state(num+1, true); // 取得num二进制有几位 int bits = getBits(num+1); // 筛选 for(int i=0; i<bits-1; i++) { int j=0; bool deleted = true; while(j<=num) { if(state[j] == true) { if(deleted == true) { state[j] = false; } deleted = !deleted; } j++; } } // 查找 for(int i=0; i<=num; i++) { if(state[i] == true) { return i; } } return -1; } // 获取num的二进制有多少位 int getBits(unsigned int num) { int res = 0; while(num > 0) { res++; num = num >> 1; } return res; }
var a=[]; for(var i=0;i<501;i++){ a.push(i); } var remove = function(arr) { if (arr.length <= 1) { return arr; } for(var i= arr.length;i>=0;i--){ if(i%2==0){ arr.splice(i,1) } } return remove(arr); } console.log(remove(a));