约瑟夫环问题的Java实现

Java 专栏收录该内容
¥59.90 ¥99.00
103篇文章

约瑟夫环问题的Java实现

约瑟夫环(Josephus problem)是一个古老的数学问题,据说由古希腊历史学家弗拉维奥·约瑟夫斯(Flavius Josephus)提出。问题的描述如下:假设有n个人围坐在一个圆形的桌子周围。从某个人开始,按照顺时针方向数k个人,然后将第k个人移除。继续数k个人,再将第k个人移除。如此循环,直到桌子上只剩下最后一个人。该问题要求找到最后剩下的那个人在初始序列中的位置。

下面是使用Java语言实现约瑟夫环问题的源代码:

import java.util.ArrayList;
import java.util.
Josephu(约瑟夫、约瑟夫环) 问题 示意图 思路分析 代码实现 package com.liuq.linkedlist; /** * Tips: * * @author Liuq * @version 2019年08月08日 */ public class Josepfu { public static void main(String[] args) { // ... 阅读详情

相关推荐

约瑟夫环问题——Java数组及循环链表实现

前言 本篇文章撰写的初衷是为了记录本人的学习以及便于本人的再次学习,并为感兴趣的读者提供一个简单的思路参考。由于本人的能力有限,所以出现错误之处尽请见谅,如若能指出错误之处,笔者不胜感激。对于思路的分析,由于本人编写的代码比较简单,未给出详细分析,读者参照代码和部分注释即可理解。 约瑟夫环故事背景: 据说著名犹太历史学家Josephus有过以下的故事:在罗马人占领乔塔帕特后,39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个

wudalanglanlang的博客 1165

java实现约瑟夫环(附完整源码)

java实现约瑟夫环(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 659

约瑟夫环问题 java代码实现(高效率)

问题来历编辑 据说著名犹太历史学家 Josephus有过以下的故事:在罗马人占领乔塔帕特后,39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1个人开始报数,每报数到第3人该人就必须自杀,然后再由下一个重新报数,直到所有人都自杀身亡为止。然而Josephus 和他的朋友并不想遵从。一开始要站在什么

7391

Python语言在工业控制中的简单应用

在工业控制中,经常需要对采集到的数据进行分析和处理,Python的这些库可以简化这一过程。传统的工业控制系统多依赖于专用的控制硬件和软件,但随着技术的发展,开放性和灵活性成为了新的需求焦点。还有,Python社区活跃,提供了大量的开源代码和模块,这对于工业控制领域来说是一个巨大的资产。Python作为一种高级编程语言,因其简洁的语法、强大的库支持和广泛的社区资源而在工业界获得了快速的发展和广泛的应用。在工业控制领域,Python的应用日益增多,这得益于其跨平台性、脚本语言的灵活性以及与硬件设备交互的能力。

2303_78014702的博客 1730

STM32H750实战:如何用CubeMX配置NAND Flash时序(附常见问题排查)

本文详细解析了STM32H750通过CubeMX配置NAND Flash时序的实战技巧,包括硬件连接、时序参数计算与优化策略,以及常见问题如RGB屏幕干扰、多片选冲突的解决方案。特别针对NAND Flash的时序配置和抗干扰设计提供了实用代码示例和优化建议,帮助开发者提升系统稳定性和性能。

weixin_29176179的博客 368

Type-C快充移动电源芯片避坑指南:IP5356配置全流程(支持PD3.0)

本文详细解析了英集芯IP5356芯片在Type-C快充移动电源中的应用,提供了从芯片选型、原理图设计、PCB布局到PD协议调试的全流程实战指南。文章重点剖析了如何避免散热、EMI及协议握手等常见设计陷阱,并分享了量产前的关键测试验证方法,旨在帮助硬件开发者高效打造稳定可靠的快充产品。

weixin_29235525的博客 886

PyCharm与MicroPython联手:打造高效ESP32开发环境

本文详细介绍了如何使用PyCharm与MicroPython打造高效的ESP32开发环境。通过配置PyCharm插件、刷写MicroPython固件以及实战示例,帮助开发者快速上手嵌入式Python开发,显著提升物联网项目的开发效率。

weixin_29169899的博客 389

AI智能体安全沙箱agentguard:为LLM代码执行筑起防火墙

在AI应用开发领域,大语言模型(LLM)驱动的智能体(Agent)能够自动生成并执行代码,极大地提升了自动化能力。然而,赋予AI直接执行系统命令的权限也带来了显著的安全风险,如恶意代码注入、敏感数据泄露和系统资源滥用。为了解决这一核心矛盾,安全沙箱技术应运而生,它通过在不可信代码与宿主系统之间建立隔离层,实现"默认拒绝、最小权限"的安全原则。agentguard项目正是这一理念的工程实践,它结合白名单机制与容器化隔离,为AI智能体提供了可配置、可审计的安全执行环境。该方案不仅适用于内部自动化工具开发,也为构

weixin_28695537的博客 303

Ubuntu声卡检测与故障排查:从基础命令到驱动修复

本文详细介绍了在Ubuntu系统中进行声卡检测与故障排查的完整流程。从使用lspci、aplay等基础命令识别硬件,到排查驱动冲突、修复权限问题,再到调整PulseAudio高级配置,提供了一套系统性的解决方案。文章结合实战案例,帮助用户快速诊断并解决Ubuntu系统常见的“无声”问题,恢复音频功能。

g2h3i4j5的博客 231

微信文件自动归档!手把手教你用OpenClaw搭个自己的“数字员工”,告别重复劳动

很多人说“AI是未来”,但未来不是靠喊口号的。OpenClaw的真正价值,是让你把重复劳动交出去,把时间留给真正需要创造力的事。这事真能干,而且不难。下载OpenClaw(GitHub仓库按我写的步骤跑一遍遇到坑,评论区留言——我亲自帮你看作者后记:上周五,我让AI自动归档了文件后,终于在5点准时下班。回家路上,我给它发了条消息:“干得漂亮”。它没回,但我知道——它在默默等我的下一条指令。

2601_94871597的博客 422

勇攀数字高峰

摘要:该题目要求在数字地形图中寻找从最低点到最高点的所有严格递增登山路径。采用DFS深度优先搜索结合回溯算法,通过标记已访问位置并检查高度差限制(0<diff≤maxDiff)来遍历所有可能路径。算法首先确定起点(最低点)和终点(最高点),然后进行DFS搜索,每步移动需满足四个方向的合法移动且高度严格递增。最终输出符合条件的路径总数。提供了C++、Java和Python三种实现方案。

面试高手的博客 648

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

深度学习工具包 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")

上一篇: Matlab保存图像的分辨率
下一篇: Spring IOC官方文档学习笔记之依赖项 Java
普通网友
博客等级 码龄3年 286粉丝 715原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值