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
相关推荐
爱coding的橙子1 小时前
每日算法刷题Day70:10.13:leetcode 二叉树10道题,用时2h
算法·leetcode·深度优先
2351617 小时前
【LeetCode】146. LRU 缓存
java·后端·算法·leetcode·链表·缓存·职场和发展
tkevinjd21 小时前
反转链表及其应用(力扣2130)
数据结构·leetcode·链表
程序员烧烤1 天前
【leetcode刷题007】leetcode116、117
算法·leetcode
Swift社区1 天前
LeetCode 395 - 至少有 K 个重复字符的最长子串
算法·leetcode·职场和发展
Espresso Macchiato1 天前
Leetcode 3710. Maximum Partition Factor
leetcode·职场和发展·广度优先遍历·二分法·leetcode hard·leetcode 3710·leetcode双周赛167
巴里巴气1 天前
第15题 三数之和
数据结构·算法·leetcode
西阳未落1 天前
LeetCode——双指针(进阶)
c++·算法·leetcode
熬了夜的程序员1 天前
【LeetCode】69. x 的平方根
开发语言·算法·leetcode·职场和发展·动态规划
Swift社区2 天前
LeetCode 394. 字符串解码(Decode String)
算法·leetcode·职场和发展