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
相关推荐
Hi李耶8 小时前
【LeetCode】4-寻找两个正序数组的中位数
算法·leetcode·职场和发展
Hi李耶11 小时前
【LeetCode】6-Z字形变换
算法·leetcode·职场和发展
AKA__Zas11 小时前
芝士算法(前缀和2.0)
java·数据结构·算法·leetcode·哈希算法·学习方法
XWalnut12 小时前
LeetCode刷题 day33
java·数据结构·算法·leetcode
闪电悠米13 小时前
力扣hot100-142.环形链表II-哈希集合与快慢指针详解
leetcode·链表·哈希算法
Tisfy13 小时前
LeetCode 0486.预测赢家:深度优先搜索(DFS)
算法·leetcode·深度优先·dfs·博弈
退休倒计时14 小时前
【每日一题】LeetCode 45. 跳跃游戏 II TypeScript
算法·leetcode·typescript
圣保罗的大教堂15 小时前
leetcode 3016. 输入单词需要的最少按键次数 II 中等
leetcode
m0_7280331316 小时前
LeetCode HOT100(技巧)
算法·leetcode·职场和发展
白白白小纯1 天前
算法篇—反转链表
c语言·数据结构·算法·leetcode