华为OD机试 - 统计每个月兔子的总数(Java 2025 B卷 100分)

AI摘要

该博客详细介绍了如何解决一个关于兔子繁殖的问题,即求解给定月份的兔子总数。这是一个典型的斐波那契数列问题,兔子从第3个月开始每个月生一只新的兔子,问题转化为计算第n个月的兔子数量。博主提供了使用Java编程的解题思路和算法源码,包括递归函数实现,并展示了运行效果。

一、题目描述

有一种兔子,从出生后第3个月起每个月都生一只兔子,小兔子长到第三个月后每个月又生一只兔子。

例如:假设一只兔子第3个月出生,那么它第5个月开始会每个月生一只兔子。

一月的时候有一只兔子,假如兔子都不死,问第n个月的兔子总数为多少?

数据范围:输入满足 1 \le n \le 31 \1≤n≤31

二、输入描述

输入一个int型整数表示第n个月。

三、输出描述

输出对应的兔子总数。

四、测试用例

测试用例1

1、输入

4

2、输出

3

3、说明

第4个月有3只兔子,1只初始兔子和2只新生兔子。

测试用例2

1、输入

10

2、输出

55

3、说明

第10个月有55只兔子,随着每个月的兔子数量递增。

五、解题思路

根据题目描述,兔子从出生后第3个月起,每个月都会生一只兔子,并且每只兔子从第三个月

华为OD机试 新系统 双机位C卷 往年真题 机考题库(持续收录中)以及算法考点详解 每一题都有详细的答题思路、详细的代码注释、样例测试,发现新题目,随时更新,全天优快云在线答疑。 阅读详情

相关推荐

华为od机考 Python 目录【E卷+A卷+B卷+C卷+D卷】

1:进入机考网页之后如果链接上写着【B卷】,就表示是B卷题库(**对应着目录中的时间这一列 2023Q2**)。在之后,大多数同学收到的应该是B卷题库。2:2022年的题库,如果时间紧迫,建议直接刷2023年新题。3:如果机试挂了,6个月之后可以再考。4:订阅专栏后,后面更新的答案都可以查看。以后更新的题目也会放到这个专栏里。5:如果对题解有问题,私信我解答。

wtswts1232的博客 2万+

华为OD机试 新系统 双机位C卷 往年真题 机考题库(持续收录中)以及算法考点详解(Python/JS/C/C++)

刷的越多,抽中的概率越大,每一题都有详细的答题思路、详细的代码注释发现新题目,随时更新。

学Java,找哪吒 5万+

2023华为od机试统一考试B卷【快递装载】

对快递的重量数组进行排序,初始化计数器count为0,装载的快递数量为0。如果总重量totalWeight小于等于货车的载重量threshold,则将计数器count加1,表示可以装载该快递。运送的快递放在大小不等的长方体快递盒中,为了能够装载更多的快递同时不能让货车超载,需要计算最多能装多少个快递。如果总重量totalWeight超过了货车的载重量threshold,则跳出循环,不再进行后续的装载。遍历快递的重量数组,对每个快递的重量进行操作。第一行输入每个快递的重量。如 5,10,2,11。

wtswts1232的博客 1091

2025认证杯数学建模第二阶段B题完整论文(代码齐全):谣言在社交网络上的传播

这需要我们深入理解抽样检测所得的次品率的不确定性及其对生产决策的影响。对于问题二的决策分析,我们可将其细分为四个关键的决策节点,分别是零件 1 的质量检测、零件 2 的质量检测、成品的质量检测,以及成品检测不合格时的拆解处理。该策略的实施,保证了在算法迭代过程中,表现最为优异的个体能够得到保留,并顺利传递至下一代,从而保障了算法在寻求最优解过程中的收敛性和优化效果。因此,我们采用假设检验的方法,将次品分布构造成 t 分布,从而确定在相应置信度的条件下,抽样次数 n 和次品数 k 所需满足的要求。

2301_81596622的博客 1007

WINCC8.1工业组态软件安装与授权配置指南

SCADA系统作为工业自动化控制的核心平台,通过实时数据采集与监控实现生产流程可视化。WINCC作为西门子旗舰级组态软件,其8.1版本凭借稳定的运行时环境在汽车制造、流程工业等领域广泛应用。本文从工业控制系统软件安装规范切入,重点解析WINCC8.1与Automation License Manager的协同工作原理,详细说明包括系统环境检查、安装包校验、授权文件配置等关键技术环节。针对工程实施中常见的内存配置不足、授权管理器报错等典型问题,提供经过现场验证的解决方案,特别适用于需要进行产线监控系统升级维护

cqwf95925的博客 1651

GEE进行Theil-Sen Median斜率估计和Mann-Kendall检验

Python实现Theil-Sen Median斜率估计和Mann-Kendall检验// 定义时间范围//NDVI图像集合// 定义每年的开始和结束日期// 加载NDVI数据集,按日期过滤,并计算最大值.max()}));// 计算Sen斜率并剪裁// 可视化Sen斜率Map.addLayer(senSlope.select('slope'), vis, "Sen斜率 ");// MK检验// 创建一个列表,包含年度NDVI图像的list。

qq_44894692的博客 965

告别Arduino IDE!用VSCode搭建Arduino UNO R3开发环境(含代码补全配置)

本文详细指导如何用VSCode替代Arduino IDE搭建Arduino UNO R3开发环境,包含代码补全配置和高效开发技巧。通过安装必要插件、配置项目结构和优化工作流,可显著提升开发效率,特别适合复杂项目开发。文章还提供了常见问题解决方案和高级功能调优方法。

weixin_30698297的博客 477

AVEVA PDMS 二次开发之PML实战:从宏到自定义窗体的效率提升

本文详细介绍了AVEVA PDMS二次开发中PML语言的应用,从基础宏编写到高级窗体开发,全面提升工程设计效率。通过实战案例展示如何利用PML实现批量操作、流程自动化和交互优化,帮助工程师快速掌握这一强大工具。

weixin_42549451的博客 338

基于Java+SpringBoot+Vue的中小企业设备管理系统的设计与开发(源码+lw+部署文档+讲解等)

💗博主介绍:✌新人博主,工作经验两年+、专注于Java、小程序技术领域和毕业项目实战✌💗🌟文末获取源码+数据库🌟

2301_76502068的博客 1382

SAP_MM模块采购价格确定控制简介

SAP MM模块采购价格确定控制是采购管理中的关键功能,系统通过"价格确定日期"判断条件记录有效性并带出正确价格。文章分析了四种常见业务场景及其对应的价格确定方案:普通采购订单(创建日期)、长期合同(交货日期)、特殊业务(手动输入)和接口下单(系统传入)。详细介绍了配置方法和前台操作步骤,并提供了不同业务类型对应的推荐控制方式汇总表。通过合理设置价格确定日期控制,企业可以确保采购价格准确性,满足各种复杂业务场景下价格管理的需求,从而提高采购管理效率和成本控制能力。

我小时候很黑的博客 736

基于YOLOv26的护栏损坏检测系统原理讲解

本文介绍了一种基于YOLOv26深度学习的护栏损坏检测系统。该系统通过车载摄像头或固定监控采集道路护栏图像,利用改进的YOLOv26算法实现高效准确的损坏检测。系统采用分层架构设计,包含数据采集、检测处理、分析评估和应用服务四个层次,能够自动识别变形、断裂、缺失等五种常见损坏类型,并评估损坏严重程度。相比传统人工检测方法,该系统大幅提高了检测效率和准确性,降低了人工成本和安全风险。关键技术包括多尺度特征融合、注意力机制和自适应非极大值抑制等创新设计,使系统在复杂道路环境下仍能保持稳定的检测性能。

一键难忘的博客 327

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

深度学习工具包 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机试 - 磁盘容量排序 - 双指针(Java 2025 A卷 100分)
下一篇: 华为OD机试 - 删除字符串中出现次数最少的字符(Java 2024 B卷 100分)
哪 吒
哪 吒 2022年度博客之星TOP 1 2022年度博客之星TOP 1 领域专家: 后端开发技术领域 领域专家: 后端开发技术领域
博客等级 码龄9年 59万+粉丝 2933原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

打赏作者

哪 吒

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

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

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

打赏作者

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

抵扣说明:

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

余额充值