递归解决卖桃子问题java
生活随笔
收集整理的這篇文章主要介紹了
递归解决卖桃子问题java
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package Class.recursion;public class SellPeach {/*1. 每次賣桃子,桃子數量 = (原有數量-1)/22. 判斷初始桃子數量需要的條件(1) 賣桃子的次數n (2) 剩余桃子的數量peach*/private int n; // n = 5, 賣桃子的次數private int leftPeaches; // leftPeaches = 8, 最后剩余的桃子數private int trades = 0; // 交易次數public SellPeach(int n, int leftPeaches) {this.n = n;this.leftPeaches = leftPeaches;}// 用for循環也可以解決問題public int BackPeach() {leftPeaches = leftPeaches * 2 + 1;trades++;if(trades < n) {BackPeach();}return leftPeaches;}}class mainn {public static void main(String[] args) {SellPeach cal = new SellPeach(5, 8);int peaches = cal.BackPeach();System.out.println(peaches);}
}
?
?
總結
以上是生活随笔為你收集整理的递归解决卖桃子问题java的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python讲义】WEEK 4
- 下一篇: 小葵花妈妈课堂开课了~~~