1. 程式人生 > >斐波那契數列-switch很有意思的一種寫法

斐波那契數列-switch很有意思的一種寫法

Java的記憶體管理. 

程式的記憶體分為heap和stack兩個部分

.區域性變數,方法呼叫,消耗stack記憶體

類的例項,陣列,消耗heap的記憶體

stack的記憶體是連續分配的, 因此分配,釋放速度很快,但是stack記憶體數量有限,windows上預設2MB, Linux 預設1MB

Heap上的記憶體是不連續分配的,在進行記憶體分配的時候首先要找到一個連續的,足夠容納所需要記憶體塊大小的空閒記憶體,然後進行分配,

因此,可能會出現大量碎片,造成總空間記憶體足夠,無法分割的情況.

詳細情況:

[email protected]:~/java$ cat Fibonacci.java

public class Fibonacci{
        public static int calculate(int value){
                switch(value){
                        case 0:
                                return 0;
                        case 1:
                                return 1;
                        case 2:
                                return 1;
                        default:
                                return Fibonacci.calculate(value-1)+Fibonacci.calculate(value-2);
                }
        }


        public static void main(String[] args)
        {
                int value;
                System.out.print("input:");
                value = Integer.parseInt(System.console().readLine());
                System.out.println("Result:" + new Integer(Fibonacci.calculate(value)).toString());
                for(int i=1; i<=value; i++){
                        System.out.print(new Integer(Fibonacci.calculate(i)).toString()+"");
                }
        }
}
[email protected]
:~/java$

相關推薦

數列-switch有意思寫法

Java的記憶體管理.  程式的記憶體分為heap和stack兩個部分 .區域性變數,方法呼叫,消耗stack記憶體 類的例項,陣列,消耗heap的記憶體 stack的記憶體是連續分配的, 因此分配,釋放速度很快,但是stack記憶體數量有限,windows上預設2MB,

數列-java程式設計:三方法實現數列

題目要求:編寫程式在控制檯輸出斐波那契數列前20項,每輸出5個數換行 //java程式設計:三種方法實現斐波那契數列 //其一方法: public class Demo2 { // 定義三個變數方法

數列非常有意思數列,由 00 和 11 開始,之後的係數就由之前的兩數相加。

斐波那契數列是一種非常有意思的數列,由 00和 11 開始,之後的斐波那契係數就由之前的兩數相加。用數學公式定義斐波那契數列則可以看成如下形式: F_0=0F0​=0 F_1=1F1​=1 F_n=F_{n-1}+F_{n-2}Fn​=Fn−1​+Fn−

python 寫數列, 複合賦值特別

a = 0 b = 1 c = 1 while c < 10: print("fibonacci is", c) c = a + b a = b b = c

[luoguP1962] 數列(矩陣快速冪)

truct ons 技術 pan opera http 快速冪 printf ble 傳送門 解析詳見julao博客連接 http://worldframe.top/2017/05/10/清單-數學方法-——-矩陣/ —&

Java 兔子問題(數列)擴展篇

aik 第一個 truct func main target htm bre trace Java 兔子問題(斐波那契數列)擴展篇 斐波那契數列指的是這樣一個數列 0, 1, 1, 2,3, 5, 8, 13, 21, 34, 55, 89, 144, ...對於這個

數列算法

string () lis temp -1 代碼 需要 cci key 今天研究了下Fibonacci算法,實現了遞歸和非遞歸兩種方式得到指定第n個的值。 代碼如下: 遞歸方式: public static int getFib(int a){ i

hdu 4549 M數列(矩陣高速冪,高速冪降冪)

else if stdlib.h article 1.0 ostream void 我們 memset font http://acm.hdu.edu.cn/showproblem.php?pid=4549 f[0] = a^1*b^0%p,f[1] = a^0*b

vijos - P1543極值問題(數列 + 公式推導 + python)

找到 span add gin python3 abi pri n) fill P1543極值問題 Accepted 標簽:[顯示標簽] 背景 小銘的數學之旅2。 描寫敘述 已知m、n為整數,且滿足下列兩個條件: ①

通過“”數列“”學習函數遞歸

range else ret bsp 方法 res ... fbi 結果 斐波那契數列:   f(0) = 0 f(1) = 1 f(2) = 1 f(3) = 2 f(4) = 3 f(5) = 8 .......f(n) = f(n - 2) + f(n - 1

[luoguP2626] 數列(升級版)(模擬)

sub std [1] 斐波那契數 == cnblogs () ios git 傳送門 模擬 代碼 #include <cmath> #include <cstdio> #include <iostream>

數列

python 練習 def bona(): while True: n = (input(‘你想打印幾個數的斐波那契數列:‘)) if not n.isdigit(): exit() a,b,m = 0,1,0

數列的遞歸和非遞歸解法

err nbsp div clas pan 斐波那契 ret ror ++ //遞歸解法 function fib(n){ if(n < 1){ throw new Error(‘invalid arguments‘); }

數列 x

技術分享 code clas 數列 tar eight 快速 記憶化 dev (一)通項公式 1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4

劍指offer-矩形覆蓋-數列(遞歸,遞推)

思考 -1 com light logs src images 數列 斐波那契數 class Solution { public: int rectCover(int number) { if(number==0 || num

數列及青蛙跳臺階問題

step popu mtd bmi article 復雜度 rec one pen 題目1: 寫一個函數,輸入n,求斐波那契(Fibonacci)數列的第n項。 斐波那契(Fibonacci)數列定義例如以下: f(n)=?????0,1,f(n

C#數列遞歸算法

oid args console nbsp bsp c# ring 數列 tel public static int Foo(int i) { if (i < 3) { retu

使用循環解決數列Fibonacci sequence

log class 兔子 斐波那契數 知識 多少 oba enc 傳遞 1 # encoding:utf-8 2 ‘‘‘ 3 Created on 2017年8月7日 4 題目:古典問題:有一對兔子,從出生後第3個月起每個月都生一對兔子, 5 小兔子長到第三個月

C#數列方法

.text 條件 class names linq ons program stat 傳遞 using System;using System.Collections.Generic;using System.Linq;using System.Text;using Sys

洛谷—— P1962 數列

inline bsp line 100% get 滿足 opera freopen aps https://www.luogu.org/problem/show?pid=1962 題目背景 大家都知道,斐波那契數列是滿足如下性質的一個數列: • f(1) =