当前位置: 首页 > news >正文

华为OD机试真题 新系统-8位LED控制器(C/C++/Py/Java/Js/Go)

8位LED控制器

华为OD机试真题 华为OD上机考试真题 4月19号 100分题型

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

题目描述

有一个8位LED控制器,包含8个LED灯(编号0-7),初始状态全灭,用8位二进制表示为:00000000。控制器可以接收以下三种指令:

  • Lx:L表示点亮操作,x表示LED的编号(0一7),操作得到的结果是:点亮第x个LED灯,把状态设为1。
  • Dx:D表示熄灭操作,x表示LED的编号(0-7),操作得到的结果是:熄灭第x个LED灯,把状态设为0。
  • Tx:T表示切换操作,x表示LED的编号(0-7),操作得到的结果是:切换第x个LED灯的状态,若状态为0则变为1,为1则变为0。

现在给定一组指令字符串(0<=长度<=1000),按照顺序解析并执行所有的指令,并返回最终8位二进制对应的整数值。
例如:“L0L1L2T1“操作表示:点亮LED0,点亮LED1,点亮LED2,切换LED1,最终二进制00000101,应整数5。

输入描述

给定一组指令字符串(0<=长度<=1000)

输出描述

返回最终8位二进制对应的整数值

用例1

输入

L0L1L2T1

输出

5

题解

思路:模拟

  1. 可以创建一个长度为8的数组bits,代表每个位置的状态初始默认设置为0
  2. 解析输入字符串,对于不同指令处理如下
    • Lx:设置bits[x] = 1
    • Dx:设置bits[x] = 0
    • Tx:设置bits[x] ^= 1, 异或运算如果当前为0变为1,1变成0
  3. 处理完所有字符串之后,使用二进制转十进制规律计算对应的整数值即可

c++

#include<iostream> #include<vector> #include<string> #include <utility> #include <sstream> #include<algorithm> #include<cmath> #include<map> using namespace std; int controlInstruct(string& instruct) { vector<int> bits(8, 0); for (int i = 0; i < instruct.size(); i+=2) { char c = instruct[i]; int pos = instruct[i+1] - '0'; // 点亮变为1 if (c == 'L') { bits[pos] = 1; // 熄灭 } else if (c =='D') { bits[pos] = 0; // 切换 } else if (c == 'T'){ bits[pos] ^= 1; } } int res = 0; for (int i = 7; i >= 0; i--) { res = res * 2 + bits[i]; } return res; } int main() { string input; getline(cin, input); int res = controlInstruct(input); cout << res; return 0; }

JAVA

import java.util.*; public class Main { public static int controlInstruct(String instruct) { int[] bits = new int[8]; for (int i = 0; i < instruct.length(); i += 2) { char c = instruct.charAt(i); int pos = instruct.charAt(i + 1) - '0'; // 点亮变为1 if (c == 'L') { bits[pos] = 1; // 熄灭 } else if (c == 'D') { bits[pos] = 0; // 切换 } else if (c == 'T') { bits[pos] ^= 1; } } int res = 0; for (int i = 7; i >= 0; i--) { res = res * 2 + bits[i]; } return res; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String input = sc.nextLine(); int res = controlInstruct(input); System.out.println(res); } }

Python

importsysdefcontrolInstruct(instruct):bits=[0]*8i=0whilei<len(instruct):c=instruct[i]pos=int(instruct[i+1])# 点亮变为1ifc=='L':bits[pos]=1# 熄灭elifc=='D':bits[pos]=0# 切换elifc=='T':bits[pos]^=1i+=2res=0foriinrange(7,-1,-1):res=res*2+bits[i]returnresif__name__=="__main__":input_str=sys.stdin.readline().strip()print(controlInstruct(input_str))

JavaScript

constreadline=require('readline');functioncontrolInstruct(instruct){letbits=newArray(8).fill(0);for(leti=0;i<instruct.length;i+=2){letc=instruct[i];letpos=instruct[i+1].charCodeAt(0)-'0'.charCodeAt(0);// 点亮变为1if(c==='L'){bits[pos]=1;// 熄灭}elseif(c==='D'){bits[pos]=0;// 切换}elseif(c==='T'){bits[pos]^=1;}}letres=0;for(leti=7;i>=0;i--){res=res*2+bits[i];}returnres;}constrl=readline.createInterface({input:process.stdin,output:process.stdout});rl.on('line',(input)=>{console.log(controlInstruct(input.trim()));});

Go

packagemainimport("bufio""fmt""os")funccontrolInstruct(instructstring)int{bits:=make([]int,8)fori:=0;i<len(instruct);i+=2{c:=instruct[i]pos:=int(instruct[i+1]-'0')// 点亮变为1ifc=='L'{bits[pos]=1// 熄灭}elseifc=='D'{bits[pos]=0// 切换}elseifc=='T'{bits[pos]^=1}}res:=0fori:=7;i>=0;i--{res=res*2+bits[i]}returnres}funcmain(){reader:=bufio.NewReader(os.Stdin)varinputstringfmt.Fscanln(reader,&input)fmt.Println(controlInstruct(input))}

C语言

#include<stdio.h>#include<string.h>intcontrolInstruct(char*instruct){intbits[8]={0};intlen=strlen(instruct);for(inti=0;i<len;i+=2){charc=instruct[i];intpos=instruct[i+1]-'0';// 点亮变为1if(c=='L'){bits[pos]=1;// 熄灭}elseif(c=='D'){bits[pos]=0;// 切换}elseif(c=='T'){bits[pos]^=1;}}intres=0;for(inti=7;i>=0;i--){res=res*2+bits[i];}returnres;}intmain(){charinput[1000];fgets(input,sizeof(input),stdin);// 去掉换行符input[strcspn(input,"\n")]=0;printf("%d\n",controlInstruct(input));return0;}
http://www.jsqmd.com/news/672210/

相关文章:

  • 官方认证|2026年国内五大正规苦荞枕公司 / 苦荞枕厂家排名,四川等地,成都晓梦纺织品有限公司综合实力遥遥领先 - 十大品牌榜
  • 别再对着二进制文件发懵了!手把手带你用UEFITool解析BIOS固件的FD/FV/FF结构
  • 官方认证|2026年国内五大正规荞麦颈椎枕厂家 / 厂商排名,四川等地,成都晓梦纺织品有限公司综合实力遥遥领先 - 十大品牌榜
  • Qwen-Turbo-BF16保姆级教程:RTX 4090上12GB显存跑满1024px生成实录
  • 从收音机到手机:三极管(BJT/FET)是如何改变我们生活的?聊聊那些经典应用电路
  • 2026年3月金果榄苗种植基地口碑揭秘,这些基地不错,白首乌苗/四叶参种子/四叶参小苗,金果榄苗种植企业哪家强 - 品牌推荐师
  • 从‘交并比’到‘完美重合’:一文读懂目标检测中IoU的进化史(附PyTorch/TensorFlow代码对比)
  • 2026高低温试验箱品牌推荐:主流厂家测评与选型指南 - 博客湾
  • 解锁Windows 10安卓生态:无需升级的跨平台革命
  • 摄影入门 | 从光到电:数码相机的成像链路解析
  • 3个关键技巧解锁FanControl风扇控制的隐藏潜力
  • 2026年3D扫描仪品牌:启源视觉为何脱颖而出? - 工业三维扫描仪评测
  • 如何用5分钟彻底告别网盘限速:八大平台直链下载助手完整教程
  • Trae+AirUI:嵌入式 UI 开发真的能提速吗?实测来了
  • STM32F103C8T6 四驱智能小车寻迹软件源代码
  • 市场价值预测:时间序列分析的实践
  • Liunx创建挂载步骤
  • 2026 年 AI 应用开发学习路线:从入门到精通,6 个月速成实战指南
  • OneForAll学习指南
  • Maven私服部署避坑指南:除了用户名密码,你的pom.xml和settings.xml里这个‘id’标签配对了么?
  • 1.AI不是魔法:一文看懂人工智能的“前世今生”
  • 非CS专业也能玩转!用OpenMV和Python实现板球平衡系统(附完整代码与PID调参心得)
  • 速腾聚创雷达点云秒变Velodyne格式:一个ROS节点搞定SLAM算法适配(Ubuntu18.04实测)
  • 一镜通古今:Rokid AI Glasses 驱动的古建筑文物全流程智能讲解终端
  • 别再只会写代码了!Pycharm 2023.3主界面这6个隐藏功能,让你效率翻倍
  • 第2课-Python基础回顾
  • 新手司机也能懂:你的车在偷偷保护你?聊聊ESP里的ABS、TCS和VDC都是啥
  • 氨基化MIL-53包覆四氧化三铁纳米颗粒,NH₂-MIL-53@Fe₃O₄ NPs,化学结构特点
  • 构建专业级视频门户:MediaCMS如何解决现代媒体管理痛点
  • 技术深度解析:如何通过OmenSuperHub精准控制惠普游戏本硬件性能