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
相关推荐
Navigator_Z39 分钟前
LeetCode //C - 1033. Moving Stones Until Consecutive
c语言·算法·leetcode
We་ct7 小时前
LeetCode 72. 编辑距离:动态规划经典题解
前端·算法·leetcode·typescript·动态规划
m0_629494738 小时前
LeetCode 热题 100-----17.缺失的第一个正数
数据结构·算法·leetcode
Tisfy8 小时前
LeetCode 0796.旋转字符串:暴力模拟
算法·leetcode·题解·模拟·字符串匹配
小雅痞9 小时前
[Java][Leetcode middle] 209. 长度最小的子数组
java·算法·leetcode
浅念-12 小时前
吃透栈:LeetCode 栈算法题全解析
数据结构·c++·算法·leetcode·职场和发展·
阿Y加油吧12 小时前
二刷 LeetCode:62. 不同路径 & 64. 最小路径和 复盘笔记
笔记·算法·leetcode
承渊政道12 小时前
【动态规划算法】(两个数组的DP问题深度剖析与求解方法)
数据结构·c++·学习·算法·leetcode·动态规划·哈希算法
加农炮手Jinx12 小时前
LeetCode 26. Remove Duplicates from Sorted Array 题解
算法·leetcode·力扣
加农炮手Jinx12 小时前
LeetCode 88. Merge Sorted Array 题解
算法·leetcode·力扣