华为OD机试真题目录点击查看: 华为OD机试2025C卷真题题库目录|机考题库 + 算法考点详解
华为OD机试2025C卷 100分题型
小明在玩一个游戏,游戏规则如下:
在游戏开始前,小明站在坐标轴原点处(坐标值为0).
给定一组指令和一个幸运数,每个指令都是一个整数,小明按照指令前进指定步数或者后退指定步数。前进代表朝坐标轴的正方向走,后退代表朝坐标轴的负方向走。
幸运数为一个整数,如果某个指令正好和幸运数相等,则小明行进步数+1。
例如:
幸运数为3,指令为[2,3,0,-5]
指令为2,表示前进2步;
指令为3,正好和幸运数相等,前进3+1=4步;
指令为0,表示原地不动,既不前进,也不后退。
指令为-5,表示后退5步。
请你计算小明在整个游戏过程中,小明所处的最大坐标值。
第一行输入1个数字,代表指令的总个数 n(1 ≤ n ≤ 100)
第二行输入1个数字,代表幸运数m(-100 ≤ m ≤ 100)
第三行输入n个指令,每个指令的取值范围为:-100 ≤ 指令值 ≤ 100
输出在整个游戏过程中,小明所处的最大坐标值。异常情况下输出:12345
2
1
-5 1
0
总共2个指令,幸运数为1,按照指令行进,依次如下游戏开始前,站在坐标轴原点,此时坐标值为0;
指令为-5,后退5步,此时坐标值为-5;
指令为1,正好等于幸运数,前进1+1=2步,此时坐标值为-3;
整个游戏过程中,小明所处的坐标值依次为[0, -5, -3],最大坐标值为0。
5
-5
-5 1 6 0 -7
1
总共5个指令,幸运数为-5,依照指令行进,依次如下:
游戏开始前,站在坐标轴原点,此时坐标值为0,
指令为-5,正好等于幸运数,后退5+1=6步,此时坐标值为-6;
指令为1,前进1步,此时坐标值为-5;
指令为6,前进6步,此时坐标值为1;
指令为0,既不前进,也不后退,此时坐标值为1;
指令为-7,后退7步,此时坐标值为-6。
整个游戏过程中,小明所处的坐标值依次为:
[0, -6, -5, 1, 1, -6],最大坐标值为1。
思路:模拟题
简单模拟题,主要注意一下几点
明白两点之后,这道题就非常简单了。具体逻辑可参照下面代码。
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main() {
int n,m;
cin >> n;
cin >> m;
// 异常
if (n < 1 || n > 100 || m < -100 || m > 100) {
cout << "12345";
return 0;
}
int res = 0;
int pos = 0;
for (int i = 0; i < n; i++) {
int command;
cin >> command;
// 异常
if (command < -100 || command > 100) {
cout << "12345";
return 0;
}
// 等于幸运数字 幸运数字等于0的情况应该还是不移动的
if (command == m) {
if (command < 0) {
command -= 1;
} else if (command > 0) {
command += 1;
}
}
pos += command;
// 尝试更新最大坐标数
res = max(pos, res);
}
cout << res;
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // 命令数量
int m = sc.nextInt(); // 幸运数字
// 异常判断
if (n < 1 || n > 100 || m < -100 || m > 100) {
System.out.println("12345");
return;
}
int res = 0;
int pos = 0;
for (int i = 0; i < n; i++) {
int command = sc.nextInt();
// 异常判断
if (command < -100 || command > 100) {
System.out.println("12345");
return;
}
// 等于幸运数字 幸运数==0是不发生移动
if (command == m) {
if (command < 0) {
command -= 1;
} else if (command > 0) {
command += 1;
}
}
pos += command;
res = Math.max(res, pos);
}
System.out.println(res);
}
}
# 读取输入
n = int(input())
m = int(input())
commands = list(map(int, input().split()))
# 异常检查
if n < 1 or n > 100 or m < -100 or m > 100 or len(commands) != n:
print("12345")
exit()
res = 0
pos = 0
for command in commands:
if command < -100 or command > 100:
print("12345")
exit()
# 幸运数字处理
if command == m:
if command > 0:
command += 1
elif command < 0:
command -= 1
pos += command
res = max(res, pos)
print(res)
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let lines = [];
rl.on('line', (line) => {
lines.push(line.trim());
if (lines.length === 3) {
let n = parseInt(lines[0]);
let m = parseInt(lines[1]);
let commands = lines[2].split(' ').map(Number);
// 输入异常
if (n < 1 || n > 100 || m < -100 || m > 100 || commands.length !== n) {
console.log("12345");
return;
}
let pos = 0;
let res = 0;
for (let command of commands) {
// 超过范围
if (command < -100 || command > 100) {
console.log("12345");
return;
}
// 处理等于幸运数的情况
if (command === m) {
if (command > 0) {
command += 1;
} else if (command < 0) {
command -= 1;
}
}
pos += command;
res = Math.max(res, pos);
}
console.log(res);
rl.close();
}
});
package main
import (
"fmt"
)
func main() {
var n, m int
fmt.Scan(&n)
fmt.Scan(&m)
// 超过范围
if n < 1 || n > 100 || m < -100 || m > 100 {
fmt.Println("12345")
return
}
commands := make([]int, n)
for i := 0; i < n; i++ {
fmt.Scan(&commands[i])
// 超过范围
if commands[i] < -100 || commands[i] > 100 {
fmt.Println("12345")
return
}
}
pos, res := 0, 0
for _, command := range commands {
// 处理等于幸运数的情况
if command == m {
if command > 0 {
command++
} else if command < 0 {
command--
}
}
pos += command
if pos > res {
res = pos
}
}
fmt.Println(res)
}