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
相关推荐
吃着火锅x唱着歌7 分钟前
LeetCode 2110.股票平滑下跌阶段的数目
数据结构·算法·leetcode
小猪咪piggy2 小时前
【算法】day2 双指针+滑动窗口
数据结构·算法·leetcode
hn小菜鸡11 小时前
LeetCode 3643.垂直翻转子矩阵
算法·leetcode·矩阵
YuTaoShao13 小时前
【LeetCode 每日一题】3000. 对角线最长的矩形的面积
算法·leetcode·职场和发展
愚润求学14 小时前
【贪心算法】day8
c++·算法·leetcode·贪心算法
岁忧19 小时前
(LeetCode 每日一题) 3541. 找到频率最高的元音和辅音 (哈希表)
java·c++·算法·leetcode·go·散列表
·云扬·1 天前
【Leetcode hot 100】101.对称二叉树
算法·leetcode·职场和发展
睡不醒的kun1 天前
leetcode算法刷题的第三十二天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
共享家95272 天前
经典动态规划题解
算法·leetcode·动态规划