2023-08-16力扣每日一题

链接:

2682. 找出转圈游戏输家

题意:

环形1到n,从1开始,每次移动 第i次*k ,当移动到出现过的序号时停下,

求没移动到的数字

解:

简单模拟题,我也以为有数学做法,可恶

实际代码:

c++ 复制代码
#include<bits/stdc++.h>
using namespace std;
vector<int> circularGameLosers(int n, int k)
{
    set<int>get;get.insert(1);
    vector<int>ans;
    int now=1,nextMove=k;
    while(true)
    {
        now=(now+nextMove-1)%n+1;
        if(get.count(now)==1) break;
        get.insert(now);
        nextMove+=k;
    }
    //cout<<endl;
    for(int i=1;i<=n;i++) if(get.count(i)==0) ans.push_back(i);
    return ans;
}
int main()
{
    int n,k;cin>>n>>k;
    vector<int>ans=circularGameLosers(n,k);
    for(auto& a:ans) cout<<a<<endl;
    return 0;
}

限制:

  • 1 <= k <= n <= 50
相关推荐
2301_807997382 小时前
代码随想录-day26
数据结构·c++·算法·leetcode
小欣加油2 小时前
leetcode 3318 计算子数组的x-sum I
c++·算法·leetcode·职场和发展
海琴烟Sunshine4 小时前
leetcode 190. 颠倒二进制位 python
python·算法·leetcode
海琴烟Sunshine4 小时前
leetcode 338. 比特位计数 python
python·算法·leetcode
被AI抢饭碗的人9 小时前
算法题(254):灾后重建
算法·leetcode·职场和发展
Kuo-Teng21 小时前
Leetcode438. 找到字符串中所有字母异位词
java·算法·leetcode
墨染点香1 天前
LeetCode 刷题【138. 随机链表的复制】
算法·leetcode·链表
Shinom1ya_1 天前
算法 day 41
数据结构·算法·leetcode
一匹电信狗1 天前
【C++】红黑树详解(2w字详解)
服务器·c++·算法·leetcode·小程序·stl·visual studio
仰泳的熊猫1 天前
LeetCode:72. 超级次方
数据结构·c++·算法·leetcode