2026华为OD机试真题 10月25日 新系统 【乘车顺序】

乘车顺序(Java /Py/C/C++/Js/Go)题解

华为OD机试真题 新系统 华为OD上机考试真题新系统 10月25号 100分题型

华为OD机试新系统真题目录点击查看: 华为OD机试新系统真题题库目录|机考题库 + 算法考点详解

题目内容

中心小学在暑假期间组织学生参观,带队老师组织学生在操场上做游戏来决定大家的乘车顺序。老师随机给出两个学号A和B,若A同学当前位置在B同学之前(或重合),则将A和B之间的学生(包含A B)按原顺序移动到队伍最前面,其余学生保持相对顺序;若A当前位置在B之后,则将队列中B之前的所有学生(不包含B)移动到队列最末尾,其余学生保持相对顺序。

学校共有X名学生(1 <= X<=1200),游戏开始时队列按学号递增排序,求经过Y轮(1 <= Y <= X<= 1200)之后的乘车顺序。

输入描述

X Y:整型格式,表示共有X名学生,完成Y轮游戏

接下来Y行,每行 A B, 表示指定的学生

输出描述

输出Y轮之后最终乘车顺序

样例1

输入

复制代码
5 3
3 5
5 1
3 4

输出

复制代码
3 4 5 1 2

题解

思路:模拟

  1. 由于Y <= X <=1200直接通过数组进行模拟每一轮叫号即可。
  2. 初始化最初学号顺序按照1....X使用数组进行存储。
  3. 对于每一轮遍历数组分别找到A的位置l,B的位置r, 然后使用备份数组进行移动
    • 对于l <= r的情况:将原数组[l,r]移动备份数组首部,将[0, l-1]紧接移动到备份数组,最后将[r+1, X-1]移动到备份数组
    • 对于r > l情况:将原数组[r, X-1]移动到备份数组首部,将[0, r-1]移动到备份数组。
  4. 按照3的规律进行每一轮操作处理即可得到最终结果。

C++

cpp 复制代码
#include <bits/stdc++.h>
#include <vector>
using namespace std;

vector<int> solve(int X, int Y, vector<vector<int>> command) {
    vector<int> a(X);
    for (int i = 0; i < X; i++) {
        a[i] = i + 1;
    }
    for (int seq = 0; seq < Y; seq++) {
        int A = command[seq][0];
        int B = command[seq][1];

        int l = -1, r = -1;

        for (int i = 0; i < X; i++) {
            if (a[i] == A) l = i;
            if (a[i] == B) r = i;
        }

        vector<int> temp;

        if (l <= r) {
            // 将 A 到 B 的区间移动到队首
            for (int i = l; i <= r; i++) {
                temp.push_back(a[i]);
            }
            for (int i = 0; i < l; i++) {
                temp.push_back(a[i]);
            }
            for (int i = r + 1; i < X; i++) {
                temp.push_back(a[i]);
            }
        } else {
            // 将 B 之前的所有学生移动到队尾
            for (int i = r; i < X; i++) {
                temp.push_back(a[i]);
            }
            for (int i = 0; i < r; i++) {
                temp.push_back(a[i]);
            }
        }

        a = temp;
    }
    return a;    
}

int main() {

    int X, Y;
    cin >> X >> Y;
    vector<vector<int>> command(Y);
    for (int i = 0; i < Y; i++) {
        int A, B;
        cin >> A >> B;
        command[i] = {A, B};
    }

    vector<int> a = solve(X, Y, command);

    for (int i = 0; i < X; i++) {
       if (i > 0) {
        cout << " ";
       }
       cout << a[i];
    }

    return 0;
}

Java

java 复制代码
import java.util.*;

public class Main {
    static int[] solve(int X, int Y, int[][] command) {
        int[] a = new int[X];
        for (int i = 0; i < X; i++) {
            a[i] = i + 1;
        }

        for (int seq = 0; seq < Y; seq++) {
            int A = command[seq][0];
            int B = command[seq][1];

            int l = -1, r = -1;

            for (int i = 0; i < X; i++) {
                if (a[i] == A) l = i;
                if (a[i] == B) r = i;
            }

            int[] temp = new int[X];
            int idx = 0;

            if (l <= r) {
                // 将 A 到 B 的区间移动到队首
                for (int i = l; i <= r; i++) {
                    temp[idx++] = a[i];
                }
                for (int i = 0; i < l; i++) {
                    temp[idx++] = a[i];
                }
                for (int i = r + 1; i < X; i++) {
                    temp[idx++] = a[i];
                }
            } else {
                // 将 B 之前的所有学生移动到队尾
                for (int i = r; i < X; i++) {
                    temp[idx++] = a[i];
                }
                for (int i = 0; i < r; i++) {
                    temp[idx++] = a[i];
                }
            }

            a = temp;
        }

        return a;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int X = sc.nextInt();
        int Y = sc.nextInt();

        int[][] command = new int[Y][2];
        for (int i = 0; i < Y; i++) {
            command[i][0] = sc.nextInt();
            command[i][1] = sc.nextInt();
        }

        int[] a = solve(X, Y, command);

        for (int i = 0; i < X; i++) {
            if (i > 0) System.out.print(" ");
            System.out.print(a[i]);
        }
        System.out.println();

        sc.close();
    }
}

Python

python 复制代码
def solve(X, Y, command):
    a = [i + 1 for i in range(X)]

    for seq in range(Y):
        A, B = command[seq]

        l, r = -1, -1

        for i in range(X):
            if a[i] == A:
                l = i
            if a[i] == B:
                r = i

        temp = []

        if l <= r:
            # 将 A 到 B 的区间移动到队首
            for i in range(l, r + 1):
                temp.append(a[i])
            for i in range(l):
                temp.append(a[i])
            for i in range(r + 1, X):
                temp.append(a[i])
        else:
            # 将 B 之前的所有学生移动到队尾
            for i in range(r, X):
                temp.append(a[i])
            for i in range(r):
                temp.append(a[i])

        a = temp

    return a


if __name__ == "__main__":
    X, Y = map(int, input().split())

    command = []
    for _ in range(Y):
        A, B = map(int, input().split())
        command.append([A, B])

    a = solve(X, Y, command)

    print(*a)

JavaScript

js 复制代码
const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', line => {
    input.push(line.trim());
});

rl.on('close', () => {
    let [X, Y] = input[0].split(' ').map(Number);

    let command = [];
    for (let i = 0; i < Y; i++) {
        command.push(input[i + 1].split(' ').map(Number));
    }

    function solve(X, Y, command) {
        let a = Array.from({ length: X }, (_, i) => i + 1);

        for (let seq = 0; seq < Y; seq++) {
            let [A, B] = command[seq];

            let l = -1, r = -1;

            for (let i = 0; i < X; i++) {
                if (a[i] === A) l = i;
                if (a[i] === B) r = i;
            }

            let temp = [];

            if (l <= r) {
                // 将 A 到 B 的区间移动到队首
                for (let i = l; i <= r; i++) {
                    temp.push(a[i]);
                }
                for (let i = 0; i < l; i++) {
                    temp.push(a[i]);
                }
                for (let i = r + 1; i < X; i++) {
                    temp.push(a[i]);
                }
            } else {
                // 将 B 之前的所有学生移动到队尾
                for (let i = r; i < X; i++) {
                    temp.push(a[i]);
                }
                for (let i = 0; i < r; i++) {
                    temp.push(a[i]);
                }
            }

            a = temp;
        }

        return a;
    }

    let a = solve(X, Y, command);
    console.log(a.join(' '));
});

Go

go 复制代码
package main

import (
    "bufio"
    "fmt"
    "os"
)

func solve(X, Y int, command [][]int) []int {
    a := make([]int, X)
    for i := 0; i < X; i++ {
        a[i] = i + 1
    }

    for seq := 0; seq < Y; seq++ {
        A := command[seq][0]
        B := command[seq][1]

        l, r := -1, -1

        for i := 0; i < X; i++ {
            if a[i] == A {
                l = i
            }
            if a[i] == B {
                r = i
            }
        }

        temp := make([]int, 0, X)

        if l <= r {
            // 将 A 到 B 的区间移动到队首
            for i := l; i <= r; i++ {
                temp = append(temp, a[i])
            }
            for i := 0; i < l; i++ {
                temp = append(temp, a[i])
            }
            for i := r + 1; i < X; i++ {
                temp = append(temp, a[i])
            }
        } else {
            // 将 B 之前的所有学生移动到队尾
            for i := r; i < X; i++ {
                temp = append(temp, a[i])
            }
            for i := 0; i < r; i++ {
                temp = append(temp, a[i])
            }
        }

        a = temp
    }

    return a
}

func main() {
    in := bufio.NewReader(os.Stdin)
    out := bufio.NewWriter(os.Stdout)
    defer out.Flush()

    var X, Y int
    fmt.Fscan(in, &X, &Y)

    command := make([][]int, Y)
    for i := 0; i < Y; i++ {
        command[i] = make([]int, 2)
        fmt.Fscan(in, &command[i][0], &command[i][1])
    }

    a := solve(X, Y, command)

    for i := 0; i < X; i++ {
        if i > 0 {
            fmt.Fprint(out, " ")
        }
        fmt.Fprint(out, a[i])
    }
    fmt.Fprintln(out)
}

C语言

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>

int* solve(int X, int Y, int command[][2]) {
    int* a = (int*)malloc(X * sizeof(int));

    for (int i = 0; i < X; i++) {
        a[i] = i + 1;
    }

    for (int seq = 0; seq < Y; seq++) {
        int A = command[seq][0];
        int B = command[seq][1];

        int l = -1, r = -1;

        for (int i = 0; i < X; i++) {
            if (a[i] == A) l = i;
            if (a[i] == B) r = i;
        }

        int* temp = (int*)malloc(X * sizeof(int));
        int idx = 0;

        if (l <= r) {
            // 将 A 到 B 的区间移动到队首
            for (int i = l; i <= r; i++) {
                temp[idx++] = a[i];
            }
            for (int i = 0; i < l; i++) {
                temp[idx++] = a[i];
            }
            for (int i = r + 1; i < X; i++) {
                temp[idx++] = a[i];
            }
        } else {
            // 将 B 之前的所有学生移动到队尾
            for (int i = r; i < X; i++) {
                temp[idx++] = a[i];
            }
            for (int i = 0; i < r; i++) {
                temp[idx++] = a[i];
            }
        }

        free(a);
        a = temp;
    }

    return a;
}

int main() {
    int X, Y;
    scanf("%d %d", &X, &Y);

    int command[1200][2];

    for (int i = 0; i < Y; i++) {
        scanf("%d %d", &command[i][0], &command[i][1]);
    }

    int* a = solve(X, Y, command);

    for (int i = 0; i < X; i++) {
        if (i > 0) {
            printf(" ");
        }
        printf("%d", a[i]);
    }

    printf("\n");

    free(a);

    return 0;
}
相关推荐
每天进步一点点km3 天前
自用-牛客网-华为机试编程模拟题1-python
华为od·华为·面试
无限码力1 个月前
华为OD面试手撕真题 【合并相邻且相等的元素】多语言题解
华为od·面试·华为od面试真题·华为od面试手撕真题·华为od面试算法真题
无限码力1 个月前
2026.9.20 华为OD机试真题 新系统 【简单 URL 路径提取器】
华为od·华为od机考·华为od机试·华为od机试真题新系统·华为od最新笔试真题题解·华为od9月20号机试真题题解
无限码力1 个月前
2026华为OD机试真题 新系统 9月27日【坚果巧克力】
算法·华为od·华为od机考·华为od机试真题·华为od机试·华为od机考真题题解·华为社招笔试真题
无限码力2 个月前
9.16华为OD机试真题 新系统 - 矩阵螺旋遍历 (Java/Py/C/C++/Js/Go)
华为od机考·华为od机试·华为od机试真题题解·华为od笔试·华为od机试真题 新系统·华为od9月16号真题
c++开发工程师2 个月前
Scrum 核心角色与分工
华为od·scrum
无限码力2 个月前
华为OD机试真题 新系统【图的遍历】
华为od·华为od机试·华为od上机考试真题·华为od机考真题·华为od机试真题题解·华为od机考真题题解·华为机试题库
哪 吒2 个月前
华为OD机试 - 云服务安全策略最优选择 - 深度优先搜索DFS(Java 新系统 200分)
java·华为od·深度优先
无限码力2 个月前
8.26华为OD机试真题 新系统【字符串回文判断】
算法·华为od·华为od机考·华为od机试·华为od上机考试真题·华为od最新机试真题题解·华为od机试真题题库