华为OD机试 C++ 阿里巴巴找黄金宝箱(I)

题目

阿里巴巴发现了一排宝箱,每个宝箱上都标有一个数字。黄金宝箱是这样一个特殊的宝箱:它前面所有宝箱的数字之和等于它后面所有宝箱的数字之和。如果第一个或最后一个宝箱是黄金宝箱,那它只需要考虑一边的和是否为0。

请帮助阿里巴巴找出这个黄金宝箱,如果找到了,返回宝箱的编号;如果没有这样的宝箱,就返回-1。

输入:

一个由逗号分隔的数字列表,代表每个宝箱上的数字。例如:1,-1,0。列表里的宝箱数量在1到10000之间。
每个宝箱上的数字范围:-1000到1000。
输出:

黄金宝箱的编号,或者-1(如果没有找到黄金宝箱)。
示例:

输入: 2,5,-1,8,6
输出: 3
原因: 下标为3的宝箱前面的数字之和 (2 + 5 - 1 = 6) 与它后面的数字之和 (6) 相等。

输入: 8,9
输出: -1
原因: 没有任何宝箱满足条件。

输入: 11
输出: 0
原因: 第一个宝箱之前和之后的数字之和都是0。

代码

#include <iostream>
华为OD机试2025A卷 - 阿里巴巴找黄金宝箱(I)(Java & Python& JS & C++ & C ) 华为OD机试2025A卷 - 阿里巴巴找黄金宝箱(I)(Java & Python& JS & C++ & C )一贫如洗的樵夫阿里巴巴在去砍柴的路上,无意中发现了强盗集团的藏宝地,藏宝地有编号从0~N的箱子,每个箱子上面贴有一个数字,箱子中可能有一个黄金宝箱。黄金宝箱满足排在它之前的所有箱子数字和等于排在它之后的所有箱子数字和;第一个箱子左边部分的数字和定义为0;最后一个宝箱右边部分的数字和定义为0。请帮阿里巴巴找到黄金宝箱,输出第一个满足条件的黄金宝箱编号,如果不存在黄金宝箱,请返回-1。 阅读详情

相关推荐

【华为OD机试】 阿里巴巴找黄金宝箱(I)(C++ Python Java)2023 B卷

洗的樵夫阿里巴巴在去砍柴的路上,无意中发现了强盗集团的藏宝地, 藏宝地有编号从0~N的箱子,每个箱子上面贴有一个数字,箱子中可能有一个黄金宝箱。

dijkstra2023的博客 216

华为OD机试 - 阿里巴巴找黄金宝箱(I) - 双指针(Python/JS/C/C++ 2025 A卷 100分)

一贫如洗的樵夫阿里巴巴在去砍柴的路上,无意中发现了强盗集团的藏宝地,藏宝地有编号从0~N的箱子,每个箱子上面贴有一个数字,箱子中可能有一个黄金宝箱。黄金宝箱满足排在它之前的所有箱子数字和等于排在它之后的所有箱子数字和;第一个箱子左边部分的数字和定义为0;请帮阿里巴巴找到黄金宝箱,输出第一个满足条件的黄金宝箱编号,如果不存在黄金宝箱,请返回-1。箱子上贴的数字列表,使用逗号分隔,例如1,-1,0。下标3之后的数字和为:10 + 2 + 3 = 15。下标3之前的数字和为:1 + 5 + 9 = 15。

学Java,找哪吒 861

【华为OD机试真题 2025B卷】290、阿里巴巴找黄金宝箱(I) | 机试真题+思路参考+代码分析(C++、Java、Py、C语言、JS)

题目描述 >一贫如洗的樵夫阿里巴巴在去砍柴的路上,无意中发现了强盗集团的藏宝地,藏宝地有编号从0~N的箱子,每个箱子上面贴有一个数字,箱子中可能有一个黄金宝箱 >  黄金宝箱满足排在它之前的所有箱子数字和等于排在它之后的所有箱子数字和;第一个箱子左边部分的数字和定义为0;最后一个宝箱右边部分的数字和定义为0 >  请帮阿里巴巴找到黄金宝箱,输出第一个满足条件的黄金宝箱编号 ,如果不存在黄金宝箱,请返回-1 ---

KJ.JK 1万+

2024年华为od机试真题题库

我是一名软件开发机构老师,我的学生已经有上百人通过了华为OD机试,2024年8月-2025年2月考的都是OD统一考试(E卷),题库已经整理好了,命中率95%以上。

面试高手的博客 9万+

华为OD机试真题2023 B卷(JAVA&JS)

华为OD机试题库已由2022Q4 A卷 切换 为2023 B卷,题目都是机试真题,已由上千位同学认证!

qq_34465338的博客 4万+

【华为OD机试B卷,GO题解独家】黄金宝箱、阿里巴巴找黄金宝箱

一贫如洗的樵夫阿里巴巴在去砍柴的路上,无意中发现了强盗集团的藏宝地,藏宝地有编号从 0~N 的箱子,每个箱子上面贴有一个数字,箱子中可能有一个黄金宝箱。请帮阿里巴巴找到黄金宝箱,输出第一个满足条件的黄金宝箱编号,如果不存在黄金宝箱,请返回-1。黄金宝箱满足排在它之前的所有箱子数字和等于排在它之后的所有箱子数字和;箱子上贴的数字列表,使用逗号分隔,例如 1,-1,0。下标 3 之前的数字和为:2 + 5 + -1 = 6。下标 0 之前的数字和为:0。下标 0 之后的数字和为:0。第一个黄金宝箱的编号。

梦想橡皮擦,专栏100例写作模式先行者,现象级专栏 《Python 爬虫 100 例》作者、《滚雪球学 Python 专栏》原创者 1万+

【OD统一考试(B卷)考生抽中题】黄金宝箱、阿里巴巴找黄金宝箱,用 C++ 编码,速通

一贫如洗的樵夫阿里巴巴在去砍柴的路上,无意中发现了强盗集团的藏宝地,藏宝地有编号从 0~N 的箱子,每个箱子上面贴有一个数字,箱子中可能有一个黄金宝箱。请帮阿里巴巴找到黄金宝箱,输出第一个满足条件的黄金宝箱编号,如果不存在黄金宝箱,请返回-1。黄金宝箱满足排在它之前的所有箱子数字和等于排在它之后的所有箱子数字和;箱子上贴的数字列表,使用逗号分隔,例如 1,-1,0。下标 3 之前的数字和为:2 + 5 + -1 = 6。下标 0 之前的数字和为:0。下标 0 之后的数字和为:0。第一个黄金宝箱的编号。

梦想橡皮擦,专栏100例写作模式先行者,现象级专栏 《Python 爬虫 100 例》作者、《滚雪球学 Python 专栏》原创者 1万+

华为OD机试 - 阿里巴巴找黄金宝箱(III)(Python/JS/C/C++ 2025 A卷 100分)

华为OD机试 2025A卷题库疯狂收录中,刷题。

学Java,找哪吒 1464

2026华为OD机试(Python)真题【E卷+A卷+B卷+C卷+D卷】目录+在线OJ

华为OD机试真题题库【E卷+A卷+B卷+C卷+D卷】目录,Python实现。

MISAYAONE的博客 29万+

【华为OD机试真题 2025B卷】316、阿里巴巴找黄金宝箱(V) | 机试真题+思路参考+代码解析(C++、Java、Py、C语言、JS)

### 🎃题目描述 >一贫如洗的樵夫阿里巴巴在去砍柴的路上,无意中发现了强盗集团的藏宝地,藏宝地有编号从0~N的箱子,每个箱子上面贴有一个数字。 阿里巴巴念出一个咒语数字k(k

KJ.JK 1万+

2023华为od机试真题B卷【阿里巴巴找黄金宝箱(V)】Java 实现

贫如洗的樵夫阿里巴巴在去砍柴的路上,无意中发现了强盗集团的藏宝地,藏宝地有编号从0-N的箱子,每个箱子上面贴有一个数字.阿里巴巴念出一个咒语数字k(k

MISAYAONE的博客 7446

DM9000有线网卡驱动编写

DM9000有线网卡驱动编写 1.DM9000简介   DM9000 是一款完全集成的、性价比高、引脚数少、带有通用处理器接口的单芯片快速以太网控制器。 自 带一个 10/100M PHY 和 4K 双字的 SRAM ,DM9000A 为适应各种处理器提供了 8 位、16 位数据接口访问 内部存储器,DM9000 拥有自动协商功能,DM9000 特性如下:   1.集成自适应 10/100M 收发器。   2.内置 16k 字节的 SRAM。   3.支持硬件帧校验。   4.兼容 3.3V 和 5.0V

weixin_44453694的博客 4673

PRIDE PPP-AR软件实战:从ZTD解算到PWV反演的全流程避坑指南

本文详细解析了使用PRIDE PPP-AR软件进行GNSS数据处理的全流程,重点涵盖从ZTD解算到PWV反演的关键步骤与精度评估。文章深入剖析了软件部署、数据准备、参数配置中的常见“坑点”,并提供了与IGS ZTD及ERA5产品进行交叉验证的实用方法,旨在帮助用户高效、准确地获取高精度大气可降水量产品。

p8q9r0的博客 613

安卓模拟器绕过检测全解析:雷电、MuMu、蓝叠、逍遥、夜神与WSA完整指南

随着手机游戏和应用的普及,越来越多的用户选择在PC上通过模拟器来运行安卓应用。然而,许多应用和游戏为了防止作弊或出于安全考虑,会检测是否运行在模拟器环境中。本文将详细介绍各大主流模拟器的检测绕过方法,帮助用户在电脑上流畅体验各类安卓应用。1. 获取模拟器系统镜像2. 使用ROM厨房工具修改系统文件3. 替换模拟器的系统镜像雷电模拟器:适合游戏,推荐使用MAGISK + EdXposed方案MuMu模拟器:适合中度游戏,推荐太极框架蓝叠模拟器:适合国际应用,推荐MAGISK方案逍遥模拟器。

专注技术分享 3万+

手把手教你用Simulink搭建DSOGI-PLL模型(附完整代码下载)

本文详细介绍了在Simulink中搭建高鲁棒性DSOGI-PLL模型的完整工程实践指南。针对传统锁相环在非理想电网下的不足,文章深入剖析了双二阶广义积分器锁相环(DSOGI-PLL)的核心原理与优势,并提供了从模块构建、系统组装到参数整定与测试验证的全流程手把手教程,附有完整代码下载,助力工程师应对谐波、电压跌落等复杂电网工况。

weixin_29223603的博客 392

西瓜书第二章——模型评估与选择(十折交叉验证/查准查全F1)

第二章——模型评估与选择

xsddys的博客 955

JAVA:Spring Boot3 集成 Spring AI 实现 Skill 开发

本文介绍了如何通过Spring Boot和Spring AI实现大模型技能(Skill)开发,让AI具备调用业务系统能力。文章首先解释了Skill的概念,即AI可调用的业务能力,如查询订单、天气等,并展示了执行流程和整体架构。然后详细介绍了实践样例,包括技术版本、Maven依赖配置、AI服务设置、ChatClient创建和Skill技能注册中心的实现。注册中心提供了技能的注册、查询、启用/禁用等功能,支持按类别管理技能,并能够获取所有可用技能列表。通过这种方式,开发者可以轻松为大模型扩展业务功能调用能力。

木头 627

华为od 面试八股文_测试/测试开发_01_含答案

华为od面试八股文_测试/测试开发_01_含答案

MISAYAONE的博客 3822

5套高颜值AI漫剧女主设定图!直接拿去做漫剧(附完整提示词)

左侧特写放大表现暖棕眼妆、豆沙唇、红宝石首饰、发丝与针织纹理,四个视图严格保持同一人物脸模、发色、服装、首饰和身体比例,柔和复古棚拍光,低饱和酒红与奶油色配色,真实布料、皮革、金属与宝石材质,高级精致角色资产设定图,高清8K。佩戴珍珠与透明水晶耳坠、纤细珍珠项链;左侧特写放大至胸部以上,清晰表现皮肤、眼妆、发丝、耳饰和针织材质,人物四个视图保持同一脸模、发型、服装、饰品和身体比例,柔和棚拍漫射光,轻微地面阴影,高级细腻材质,真实发丝,细腻皮肤,高精度角色资产设定图,高清8K质感。

欢迎来到鸾姝淡月博客,专为程序员和编程爱好者打造的技术博客。我们分享编程语言趋势、开发工具、编程哲学、职业发展等,助你掌握新技术,优化技能,激发编程热情。 1868

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: 华为od机试 Python 地址分割
下一篇: 华为OD机试 C++【 最接近的数】
AlgorithmHero
博客等级 码龄4年 606粉丝 1067原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AlgorithmHero

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值