JAVA斐波那契(Fibonacci)数列

Fibonacci斐波那契数列-Java解法 1 问题描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。 n<=39 F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2) 2 递归解法 public class Solution { public int Fibonacci(int n) { /* 方法1 递归 */ ... 阅读详情

Fibonacci数列大家都了解过了吧,其公式就是F(n) = F(n-1) + F(n - 2)
那么这次我们就把他稍微修改一下,F[1] = 1 F[2] = 2 F[3] = 3 对于F(n)为F(n) = F(n-1)+F(n-2)+F(n-3) 现在我们需要得到F(n)的结果 使用分治思想来完成它吧

package com.jianning.fibonacci;
/*
 * 题目描述
Fibonacci数列大家都了解过了吧,其公式就是F(n) = F(n-1) + F(n - 2)
那么这次我们就把他稍微修改一下,F[1] = 1 F[2] = 2 F[3] = 3 对于F(n)为F(n) = F(n-1)+F(n-2)+F(n-3) 现在我们需要得到F(n)的结果 使用分治思想来完成它吧!
 */
public class Fibonacci {
	public static long value(int index) {//传入角标,返回F(n)
		long[] arr = new long[index];
		arr[0] = 1;
		arr[1] = 2;
		arr[2] = 3;
		for(int i = 3;i < index;i++) {
			arr[i] = arr[i-1] + arr[i-2] + arr[i-3];
		}
		return arr[index-1];
	}
	public static long fibSUM(int index) {//传入角标,返回斐波那契数列所有值的和(附加)
		long[] arr = new long[index];
		arr[0] = 1;
		arr[1] = 2;
		arr[2] = 3;
		long sum = arr[0] + arr[1] +arr[2];
		for(int i = 3;i < index;i++) {
			arr[i] = arr[i-1] + arr[i-2] + arr[i-3];
			sum = sum +arr[i];
		}
		return sum;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int x = 4;//范围4-30
		System.out.println("斐波那契数列F("+x+")的值为:"+value(x));
		System.out.println("斐波那契数列前"+x+"的和为:"+fibSUM(x));
	}

}

树莓派与ROS2 树莓派与ROS2(Robot Operating System 2)的结合使用为机器人开发、自动化控制以及物联网项目提供了强大的平台。 阅读详情

相关推荐

迪文屏【0】——从零搭建工程:图片显示、文本变量配置与串口数据交互实战

本文以迪文屏T5L系列为例,详细介绍了从零搭建HMI工程的完整流程。内容涵盖硬件软件准备、DGUS工程创建、背景图片显示、文本变量配置,并重点演示了如何通过串口指令实时修改屏幕数据变量,实现动态交互。旨在帮助开发者快速掌握迪文屏开发的核心步骤与数据交互方法。

alpha的博客 359

Java——斐波拉契数列

【代码】Java——斐波拉契数列。

ayddyn的博客 156

[V-10]ARMv8/v9-CPU Virtualization-CPU虚拟化架构简介(vCPU/vPE)

本文简要介绍了CPU虚拟化架构的基础知识。

基层架构师的分享 1270

Java两种方式打印斐波那契数列

package homework; public class FibonacciSequence { public static void main(String[] args) { int fiponatch[] = new int[15]; /** * 方法一 */ fiponatch[0] = 1; fiponatch[1] = 1; System.out.println("方法一输出斐波那契数列:"); for (int i = 0; i < fip

WanAkiko. 1328

DeepSpeed Zero3 Offload实战:从GCC版本检查到环境配置的完整避坑指南

本文是一份针对DeepSpeed Zero3 Offload部署的完整避坑指南。针对训练中常见的`AttributeError: ‘DeepSpeedCPUAdam‘ object has no attribute ‘ds_opt_adam`报错,文章深入剖析了其根源在于GCC编译器版本过低。指南提供了从系统GCC版本深度探查、安全启用高版本GCC环境,到DeepSpeed一致性安装验证的全链路实战方案,旨在帮助用户构建稳定的大模型训练环境。

weixin_29070721的博客 478

基于FaceX-Zoo实现的Face Mask Adding人脸戴口罩

人脸系列文章目录 文章目录人脸系列文章目录前言一、加口罩效果二、实现原理二、代码实现 前言 目前人脸加口罩均为在人脸位置贴图式的,非常不真实。前段时间京东开源一个基于PRnet实现的3D口罩拟合人脸,加口罩非常逼真,但是只是单独一个加口罩demo,没有完整的流程。于是本人结合人脸检测,关键点对齐。PRnet加口罩实现一个完整的加口罩流程。只需一个命令简单方便调用,实现人脸加口罩。 https://github.com/JDAI-CV/FaceX-Zoo 一、加口罩效果 下图可以看出效果还是非常逼真的

zengwb的博客 3395

小小算法程序,斐波那契数列的数组实现

/** * 用数组存储一个斐波那契数列 * @author wisdom_j * */ public class Fibonacci { public static void main(String[] args) { fibonacci(); } //为了防止出现数字太大,long型不能正确 public static void fibonacci(){

一步一个脚印 1159

DataEaseV2.9 本地开发环境搭建与调试指南

本文详细指导如何搭建DataEase V2.9的本地开发环境,涵盖从软件准备、源码获取、数据库初始化到在IDEA中配置、编译和调试的全过程。重点解决了本地运行环境的关键配置与常见问题,帮助开发者高效开启DataEase的定制化开发与二次开发。

weixin_29290863的博客 146

java实现斐波那契数列求解办法

斐波那契数列最早是根据兔子繁殖问题而产生的,大致有这样的一个数列:1,1,2,3,5,8,13,。。。,其中第一、第二项固定为1,后面每一项都是前面两项之和。使用数学公式就是f(n) = f(n-1)+f(n-2)。 直观来看,最简单的做法就是通过递归求解。 public static long fib1(int n) { if(n<1) return -1; if(n==1||n==2) return 1; return fib1(n-2)+fib1(n-1);...

feinifi的博客 4905

MySQL——cost成本计算之MySQL如何选择使用索引(trace)

文章目录前言trace工具分析执行计划 前言 在之前博客中,根据测试案例,发现同样的sql语句,因为传入的条件值的不同,导致MySQL有时候选择使用索引,有时候又不适用索引。 如下所示sql的执行计划: EXPLAIN SELECT * FROM employees WHERE name > 'aaa'; EXPLAIN SELECT * FROM employees WHERE name > 'zzzz'; 在MySQL中,为什么会出现上述这种玄乎的现象呢? 本篇博客重点说明原因。 在说

专注写bug 691

002、搭建你的第一个Agent开发环境:Python与核心库安装

在上一篇《Agent智能体开发入门:概念、应用与前景》中,我们描绘了Agent智能体的宏伟蓝图,理解了其作为“数字员工”的巨大潜力。然而,许多开发者在迈出第一步时,常常被复杂的依赖、版本冲突和晦涩的配置所劝退,空有想法却无从下手。这种感觉就像拿到了一张藏宝图,却找不到出发的港口。本文正是你的“启航指南”。我们将彻底解决这个痛点,通过一个清晰、可复现的流程,为你搭建一个专为Agent开发量身定制的Python环境。一个纯净、隔离的Python虚拟环境,避免项目间的“依赖地狱”。一套经过验证的核心工具链。

G_RE_RE的博客 113

七种方式求斐波那契(Fibonacci)数列通项

一:递归实现    使用公式f[n]=f[n-1]+f[n-2],依次递归计算,递归结束条件是f[1]=1,f[2]=1。 二:数组实现    空间复杂度和时间复杂度都是0(n),效率一般,比递归来得快。 三:vector实现    时间复杂度是0(n),时间复杂度是0(1),就是不知道vector的效率高不高,当然vector有自己的属性会占用资源。 四:queue实现    当然

SzMing 1287

MES软件:Siemens Opcenter二次开发

或者在项目中右击“引用”(References),选择“添加引用”(Add Reference),浏览并选择SDK中的DLL文件。在“签名”(Signing)选项卡中,选择“为程序集签名”(Sign the assembly),并创建一个新的强名称密钥文件。在Visual Studio中,选择“测试”(Test)菜单中的“运行所有测试”(Run All Tests)。在Visual Studio中,选择“生成”(Build)菜单中的“发布”(Publish)。按照向导的提示,选择安装路径和组件。

kkchenjj的博客 1678

Java实现斐波那契数列Fibonacci

import java.util.Scanner; public class Main1 { static class Fibonacci{ int Fobonacci2(int n){ if (n==0) return 0; else if (n==1) return 1; ...

大哥的叔的博客 1万+

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

jre9下载 jdk9

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

Java练习题-输出斐波那契(Fibonacci)数列

Java练习题-输出斐波那契(Fibonacci)数列,持续更新【Java练习题】系列专栏

hacker707的博客 9592

深度学习工具包 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&#39;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(&#39;DeepLearnToolbox&#39;));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(&#39;state&#39;,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&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,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 = &#39;sigm&#39;;%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, &#39;Too big error&#39;);```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(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;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)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;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, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% 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&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 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, &#39;Too big error&#39;);```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(&#39;state&#39;,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, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,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, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,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, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % 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, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % 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, &#39;Too big error&#39;);%% 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(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % 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, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

斐波那契数列(Fibonacci)的三种JAVA解法

斐波那契数列(Fibonacci)JAVA解法 1.递归函数: public class Main { public int f(int n){ if (n == 0 | n==1) return 1; else return (f(n-1)+f(n-2)); } public static void main(String[] args) { Scanner scanner = new S

x661716的博客 4475

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

)JAVA解法:斐波那契数列(Fibonacci

斐波那契数列(Fibonacci)JAVA解法: import java.util.Scanner; public class OutFibonacci { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n = sc.nextInt(); sc.close(); //主动关闭scanner 会节省时间; int f1=

x661716的博客 283

使用Java打印斐波那契数列的简单方法——Fibonacci数列

斐波那契数列(Fibonacci sequence):这个数列从第3项开始,每一项都等于前两项之和。 @Test public void test1() { Scanner sc=new Scanner(System.in); System.out.println("输入需要斐波那契数列的个数字:"); int b = sc.nextInt(); int[] a=n...

9426474的博客 685

[Java经典题目]求Fibonacci斐波那契数列

斐波那契数列有如下特点: 第1,2个数为1、1。从第3个数开始,每个数是其前两个数之和。即 F1 = 1 (n =1) F2 =1 (n = 2) F3 = F1 + F2 = 1 +1 =2 ………… Fn = Fn-1+ Fn-2(n >=3) 关于斐波那...

weixin_34402408的博客 457

python和Java实现斐波那契Fibonacci数列

斐波那契数列大家都是很熟悉的,这里就不多说了,写这个纯碎就是把之前的算法什么的都在来一遍温习一下,关于斐波那契的讲解网上有很多的原理和实现,这里我推荐一个不错的讲解。     关于斐波那契数列有两种版本: 第一种:0,1,1,2,3,5,8,... 第二种:1,1,2,3,5,8,13,...     两种版本区别也仅仅是在首项的不同,所以如果是做题目的话,根据需要选择吧,下面是pytho

Together_CZ的博客 3721

求fibonacci数列第n项java_求斐波那契数列的第n项

问题描述:斐波那契数列是这样的一个数列,1,1,2,3,5,8,..,即前两项都是1,后面每一项都是其前面两项的和。现在要你求出该数列的第n项。分析:该问题是一个经典的数列问题,相信大家在很多语言的教科书上都碰到过这个练习题目。这里我给大家总结了三种经典解法,并对这三个方法进行了对比。解法一:递归算法。很多教科书上都用这个题作为函数递归知识点讲解的例题,我们可以将每一个项的求法表达为这样一个式子:...

weixin_39673471的博客 734

Java入门:带你认识“斐波那契数列”(Fibonacci)!

Java入门:带你认识“斐波那契数列”(Fibonacci)! 该数列的前两项是0和1,以后各项都是其前两项值之和。代码中使用了while循环来计算并输出Fibonacci数列。

m0_69414996的博客 774

JAVA编程:斐波那契数列(Fibonacci)

JAVA编程:斐波那契数列(Fibonacci)

weixin_45998941的博客 1529

java解决斐波那契数列(Fibonacci sequence)

斐波那契数列(Fibonacci sequence)的定义:斐波那契数列指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946, 17711,28657,46368........,这个数列从第3项开始,每一项都等于前两项之和。 ...

weixin_63835553的博客 698
上一篇: JAVA数据结构之稀疏数组
下一篇: JAVA实现链表(数据结构)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值