乘车顺序(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
题解
思路:模拟
- 由于
Y <= X <=1200直接通过数组进行模拟每一轮叫号即可。 - 初始化最初学号顺序按照
1....X使用数组进行存储。 - 对于每一轮遍历数组分别找到A的位置
l,B的位置r, 然后使用备份数组进行移动- 对于
l <= r的情况:将原数组[l,r]移动备份数组首部,将[0, l-1]紧接移动到备份数组,最后将[r+1, X-1]移动到备份数组 - 对于
r > l情况:将原数组[r, X-1]移动到备份数组首部,将[0, r-1]移动到备份数组。
- 对于
- 按照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;
}