2024年华为OD E卷

第一题(一星题)

输入两个正整数n,m,编号1~n围成一圈的人,从1开始报数,数到m,m就退出,接着从下一个编号从1开始报数,当剩下的人少与m时停止报数,并按从小到大的顺序输出剩下的人在原来的位置的编号?

例1:

输入:100

3

输出:58,91

例2:

输入:100

4

输出:34,45,97

使用java编码,时间 < 1ms, 空间 < 256MB:

java 复制代码
 private static void count(Scanner scanner) {
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int[] person = new int[n];
        int length = person.length;
        for (int i = 0; i < n; i++) {
            person[i] = i + 1;
        }
        int count = 0;
        for (int i = 0; i < length; i++) {
            if (person[i] == 0){
                if (i == length - 1) {
                    i = -1;
                }
                continue;
            }
            if (++count == m) {
                person[i] = 0;
                if (--n < m){
                    break;
                }
                count = 0;
            }
            if (i == length - 1) {
                i = -1;
            }
        }
        for (int no : person) {
            if (no != 0) {
                if (--n == 0) {
                    System.out.println(no);
                } else {
                    System.out.print(no + ",");
                }
            }
        }
    }
相关推荐
漫随流水15 小时前
leetcode算法(151.反转字符串中的单词)
数据结构·算法·leetcode
ada7_15 小时前
LeetCode(python)78.子集
开发语言·数据结构·python·算法·leetcode·职场和发展
一路往蓝-Anbo18 小时前
STM32单线串口通讯实战(五):RTOS架构 —— 线程安全与零拷贝设计
c语言·开发语言·stm32·单片机·嵌入式硬件·观察者模式·链表
POLITE320 小时前
Leetcode 19. 删除链表的倒数第 N 个结点 JavaScript (Day 11)
javascript·leetcode·链表
你怎么知道我是队长21 小时前
C语言---指针
c语言·数据结构·算法
萤虫之光1 天前
有序数组中的单一元素(一)
数据结构·算法
optimistic_chen1 天前
【Redis 系列】常用数据结构---SET类型
linux·数据结构·数据库·redis·set·数据类型·命令行
漫随流水1 天前
leetcode算法(344.反转字符串)
数据结构·算法·leetcode
松涛和鸣1 天前
44、HTML与HTTP服务器交互笔记
linux·运维·服务器·http·链表·html
sin_hielo1 天前
leetcode 1411(递推)
数据结构·算法·leetcode