遞歸原來可以so easy|-連載(3)

本期我們再通過幾個例子,加深遞歸的理解和熟練度。

上期有一個練習題:用遞歸逆序輸出一個包含整型數據的鏈表。

先完成這個練習題。

對于程序員來說,代碼是最好的溝通工具,什么都不說,上代碼:

public class Hello {    public static void main(String[] args) {LinkedList list=createLinkedList();//list.print(); 正序輸出list.revertPrint();//逆序輸出}   /*創建一個鏈表,用來測試*/public static LinkedList createLinkedList() {LinkedList list=new LinkedList();for(int i=11;i<=20;i++) {list.add(i);}return list;}
}package test;/*** 具有逆序輸出功能的鏈表類*/
public class LinkedList {// 內部類Node,代表鏈表的節點private class Node {Node next; // 指針域public int data;// 數據域public Node(int data) {this.data = data;this.next = null;}}// ----public Node head = null; // 鏈表頭// 向鏈表中插入數據public void add(int data) {Node nodeNew = new Node(data);if (null == head) {head = nodeNew;return;}Node pre = head;Node p = head.next;while (p != null) {pre = p;p = p.next;}pre.next = nodeNew;}// 正序輸出public void print() {Node p = head;while (p != null) {System.out.print(p.data + " ");p = p.next;}}// 逆序輸出public void revertPrint() {rP(head);}// 遞歸函數public void rP(Node node) {if (null == node)return;rP(node.next);System.out.print(node.data + " ");}
}

整數倒序輸出

如:

輸入整數1234,輸出為4321

輸入整數7890,輸出為0987

解題:

可以這么來看:

先輸出該數的個位數,

然后把此數/10后的到的商,再輸出此商的個位。

以此遞推下去,直到商為0( 也即最高位/10的商 )。

代碼:

    public static void main(String[] args) {Scanner sc=new Scanner(System.in);System.out.println("請輸入一個整數");int n=sc.nextInt();if(n==0) {System.out.println(0);return;}else if(n<0) {System.out.print('-');n=0-n;}p(n);}static void p(int n) {if(n==0)return;System.out.print(n%10);p(n/10);}

思考題:

輸出一個正整數的各位數字之和。

例如:輸入 1234,輸出10。

參考整數逆序輸出的思路。

走樓梯

問題描述

樓梯有N階,上樓可以一步上一階,也可以一次上二階。編一個程序,計算共有多少種不同的走法。

解題:

分析一下:
一階階梯:只有1種走法
二階階梯:有2種走法: 一次走2個階梯,或者2次都走一個階梯
三階階梯:
有2種走法:
先走1個階梯,則還剩3-1=2個階梯,走法數等于二階階梯的走法數量
先走2個階梯,則還剩3-2=1個階梯,走法數等于一階階梯的走法數量
根據加法原理:f3 = f(3-1)+f(3-2)
四階階梯:
有2種走法:
先走1個階梯,則還剩4-1=3個階梯,走法數等于三階階梯的走法數量
先走2個階梯,則還剩4-2=2個階梯,走法數等于二階階梯的走法數量
根據加法原理:f3 = f(4-1)+f(4-2)
以此類推,得到計算公式為:
f(n)=f(n-1)+f(n-2)
f(1)=1
f(2)=2

代碼:

    public static void main(String[] args) {Scanner sc=new Scanner(System.in);System.out.println("請輸入一個正整數,代表樓梯階數");int n=sc.nextInt();int step=stair(n);System.out.println(step);}static int stair(int n){if(n==1)return 1; //一階階梯,只有1種走法if(n==2)return 2; //二階階梯,有2種走法return stair(n-1)+stair(n-2);}

轉載于:https://blog.51cto.com/13477015/2319353

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389165.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389165.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389165.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Viewport3D 類Viewport3D 類Viewport3D 類

.NET Framework 類庫Viewport3D 類更新&#xff1a;2007 年 11 月為三維可視內容提供呈現圖面。命名空間&#xff1a; System.Windows.Controls程序集&#xff1a; PresentationFramework&#xff08;在 PresentationFramework.dll 中&#xff09;用于 XAML 的 XMLNS&#xf…

升級android 6.0系統

How to Fix errors by manually flashing Marshmallow update 鏡像下載for nexus Factory image Step 1: Download the [Marshmallow factory image](http://www.theandroidsoul.com/download-mra58k-android-6-0-marshmallow-update-released-for-nexus-5-nexus-6-nexus-9-n…

AGC 022 B - GCD Sequence

題面在這里&#xff01; 鍛煉腦子的小構造題。。。 一開始被 a[]<30000 且 序列 gcd 1所困擾&#xff0c;但是發現這并沒有什么&#xff0c;因為我接下來發現了一種總是能構造出 序列和是6的倍數的方案。 首先如果 n3 的話輸出樣例&#xff0c;因為只有這種情況沒法用我的方…

最接近原點的 k 個點_第K個最接近原點的位置

最接近原點的 k 個點In this article, I will be explaining to you one of the problems that you may find when tackling questions in data structures and algorithm. You will need some basic knowledge of data structures in order to understand the optimized solut…

網絡瀏覽器如何工作

Behind the scenes of modern Web Browsers現代Web瀏覽器的幕后花絮 The Web Browser is inarguably the most common portal for users to access the web. The advancement of the web browsers (through the series of history) has led many traditional “thick clients”…

讓自己的頭腦極度開放

為什么80%的碼農都做不了架構師&#xff1f;>>> 一. 頭腦封閉和頭腦開放 頭腦封閉 你是否經常有這樣的經歷&#xff0c;在一次會議或者在一次小組討論時&#xff0c;當你提出一個觀點而被別人否定時&#xff0c;你非常急迫地去反駁別人&#xff0c;從而捍衛自己的尊…

簡介DOTNET 編譯原理 簡介DOTNET 編譯原理 簡介DOTNET 編譯原理

簡介DOTNET 編譯原理 相信大家都使用過 Dotnet &#xff0c;可能還有不少高手。不過我還要講講Dotnet的基礎知識&#xff0c;Dotnet的編譯原理。 Dotnet是一種建立在虛擬機上執行的語言&#xff0c;它直接生成 MSIL 的中間語言&#xff0c;再由DotNet編譯器 JIT 解釋映象為本機…

RecyclerView詳細了解

關于RecyclerView大家都不陌生了&#xff0c;它的使用也越來越受歡迎&#xff0c;現在總體了解一下RecyclerView的作用&#xff0c;為什么會有RecyclerView呢&#xff0c;我用ListView也能干所有的事情啊&#xff0c;尺有所短&#xff0c;寸有所長&#xff0c;先來看看Recycler…

案例與案例之間的非常規排版

In 1929 the Cond Nast publishing group brought Russian-born Mehemed Fehmy Agha—who had been working for the German edition of Vogue magazine—to America as art director for House & Garden, Vanity Fair, and the senior edition of Vogue.1929年&#xff0c…

熊貓分發_熊貓新手:第二部分

熊貓分發This article is a continuation of a previous article which kick-started the journey to learning Python for data analysis. You can check out the previous article here: Pandas for Newbies: An Introduction Part I.本文是上一篇文章的延續&#xff0c;該文…

淺析微信支付:申請退款、退款回調接口、查詢退款

本文是【淺析微信支付】系列文章的第八篇&#xff0c;主要講解商戶如何處理微信申請退款、退款回調、查詢退款接口&#xff0c;其中有一些坑的地方&#xff0c;會著重強調。 淺析微信支付系列已經更新七篇了喲&#xff5e;&#xff0c;沒有看過的朋友們可以看一下哦。 淺析微信…

view工作原理-計算視圖大小的過程(onMeasure)

view的視圖有兩種情況&#xff1a; 內容型視圖&#xff1a;由視圖的內容決定其大小。圖形型視圖&#xff1a;父視圖為view動態調整大小。 ### measure的本質 把視圖布局使用的“相對值”轉化成具體值的過程&#xff0c;即把WRAP_CONTENT,MATCH_PARENT轉化為具體的值。 measur…

[轉載]使用.net 2003中的ngen.exe編譯.net程序

ngen.exe程序為.net 2003自帶&#xff0c; 在&#xff1a;/windows/microsoft.net/framework/v1.1.4322目錄下ngen.exe ngen能把.net框架的東西編譯成機器碼.... 網友&#xff1a;Visual Studio .NET 2003程序的運行速度怎么樣&#xff0c;我有一個感覺&#xff0c;V…

基于Redis實現分布式鎖實戰

背景在很多互聯網產品應用中&#xff0c;有些場景需要加鎖處理&#xff0c;比如&#xff1a;秒殺&#xff0c;全局遞增ID&#xff0c;樓層生成等等。大部分的解決方案是基于DB實現的&#xff0c;Redis為單進程單線程模式&#xff0c;采用隊列模式將并發訪問變成串行訪問&#x…

數據分析 績效_如何在績效改善中使用數據分析

數據分析 績效Imagine you need to do a bank transaction, but the website is so slow. The page takes so much time to load, all you can see is a blue circle.想象您需要進行銀行交易&#xff0c;但是網站是如此緩慢。 該頁面需要花費很多時間來加載&#xff0c;您只能看…

隱私策略_隱私圖標

隱私策略During its 2020 Worldwide Developers Conference, Apple spent time on one of today’s hottest topics — privacy. During the past couple of years, Apple has been rolling out various public campaigns aiming to position itself as a company that respect…

Java中的數組

一、數組的定義type[] arrayName;type arrayName[]; 推薦第一種 二、數組的初始化 含義&#xff1a;所謂的初始化&#xff0c;就是為數組的數組元素分配內存空間&#xff0c;并為每個數組元素賦初始值 &#xff08;1&#xff09;靜態初始化&#xff1a;arrayName new type[…

您一直在尋找5+個簡單的一線工具來提升Python可視化效果

Insightful and aesthetic visualizations don’t have to be a pain to create. This article will prevent 5 simple one-liners you can add to your code to increase its style and informational value.富有洞察力和美學的可視化不必費心創建。 本文將防止您添加到代碼中…

用C#編寫的代碼經C#編譯器后,并非生成本地代碼而是生成托管代碼

用C#編寫的代碼經C#編譯器后&#xff0c;并非生成本地代碼而是生成托管代碼。也就是說&#xff0c;程序集在打包時是連同CLR一起打包的。在客戶端的機器上&#xff0c;CLR一行行的讀取IL&#xff0c;在讀取每行IL時&#xff0c;CLR利用JIT編譯器將IL編譯成本地的CPU指令。若要節…

figma 安裝插件_彩色濾光片Figma插件,用于色盲

figma 安裝插件So as a UX Designer, it is important to design with disabilities in mind. One of these is color blindness. It is important to make sure important information on your product is legible to everyone. This is why I like using this tool:因此&…