小鹏算法 一面凉经
2021-11-2
介绍
项目,细节
代码
约瑟夫环
https://leetcode-cn.com/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof/
/* * m个人坐成一个圈,从0开始数,数到n,把这个人踢掉,然后继续从0开始,直到最后剩下n-1个人,输出这n-1个人 * */ #include "../common.h" void test(vector<pair<int, bool>>& vec, int n){ int len = vec.size(); int len1 = vec.size(); int count = 0; int p = 0; while(len>=n){ while(count<n){ if(p==len1){ p = 0; continue; } if(vec[p].second){ p++; } else{ p++; count++; } } if(p==len1){ p=0; } while(vec[p].second){ p++; if(p==len1){ p=0; } } vec[p].second = true; // cout << "p: " << vec[p].first << endl; len--; // cout << "len: " << len << endl; count = 0; p++; } for(auto& ele:vec){ if(!ele.second){ cout << ele.first << ", "; } else{ ; } } cout << endl; } int main(){ vector<pair<int, bool>> nums = {{1, false},{2, false}, {3, false}, {4, false}, {5, false}, {6, false}}; test(nums, 3); return 0; }#小鹏汽车##面经#