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
相关推荐
Allen Wurlitzer25 分钟前
算法刷题记录——LeetCode篇(1.9) [第81~90题](持续更新)
算法·leetcode·职场和发展
阳洞洞30 分钟前
leetcode 377. Combination Sum IV
算法·leetcode·动态规划·完全背包问题
想跑步的小弱鸡5 小时前
Leetcode hot 100(last day)
算法·leetcode·哈希算法
龙俊杰的读书笔记10 小时前
[leetcode] 面试经典 150 题——篇9:二叉树(番外:二叉树的遍历方式)
数据结构·算法·leetcode·面试
Swift社区11 小时前
从表格到序列:Swift 如何优雅地解 LeetCode 251 展开二维向量
开发语言·leetcode·swift
飞川撸码15 小时前
【LeetCode 热题100】73:矩阵置零(详细解析)(Go语言版)
leetcode·矩阵·golang
小美爱刷题17 小时前
力扣DAY40-45 | 热100 | 二叉树:直径、层次遍历、有序数组->二叉搜索树、验证二叉搜索树、二叉搜索树中第K小的元素、右视图
数据结构·算法·leetcode
熬夜造bug18 小时前
LeetCode Hot100 刷题笔记(2)—— 子串、普通数组、矩阵
笔记·leetcode·矩阵
lvchaoq19 小时前
图解力扣回溯及剪枝问题的模板应用
leetcode·深度优先·剪枝·回溯·递归
Swift社区20 小时前
LeetCode 252 会议室题全解析:Swift 实现 + 场景还原
算法·leetcode·swift