JavaScriptはフィボナッチの数列(ウサギの繁殖問題)を実現します.

6735 ワード

2016年の最初の勤務日は、JavaScriptでフィボナッチの数列を実現しました.この数列は規則を理解すればよく実現できます.
HTML    :
1
<input type="text" value="" id="num"> 2 <button onclick="test()"> button>
3 <div id="txt">div>
JavaScript    :
1
<span style="color:#008080;"> 2</span> <span style="color:#0000ff;">function</span><span style="color:#000000;"> test(){ </span><span style="color:#008080;"> 3</span> <span style="color:#0000ff;">var</span> obj = $("#num").val();<span style="color:#008000;">//</span><span style="color:#008000;"> </span> <span style="color:#008080;"> 4</span> <span style="color:#0000ff;">function</span><span style="color:#000000;"> rabbit(obj){ </span><span style="color:#008080;"> 5</span> <span style="color:#0000ff;">var</span> a = 1<span style="color:#000000;">; </span><span style="color:#008080;"> 6</span> <span style="color:#0000ff;">var</span> b = 1<span style="color:#000000;">; </span><span style="color:#008080;"> 7</span> <span style="color:#0000ff;">var</span> str = ""; <span style="color:#008000;">//</span><span style="color:#008000;"> </span> <span style="color:#008080;"> 8</span> <span style="color:#0000ff;">if</span>(obj<=1){ <span style="color:#008000;">//</span><span style="color:#008000;"> 1,</span> <span style="color:#008080;"> 9</span> str = " 1 1"+"<br/>"<span style="color:#000000;">; </span><span style="color:#008080;">10</span> $("#txt").html(str); <span style="color:#008000;">//</span><span style="color:#008000;"> 1</span> <span style="color:#008080;">11</span> }<span style="color:#0000ff;">else</span> <span style="color:#0000ff;">if</span>(obj<=2){ <span style="color:#008000;">//</span><span style="color:#008000;"> 2</span> <span style="color:#008080;">12</span> str = " 1 1"+"<br/>"+" 2 1"+"<br/>"<span style="color:#000000;">; </span><span style="color:#008080;">13</span> $("#txt").html(str); <span style="color:#008000;">//</span><span style="color:#008000;">id txt div str ;</span> <span style="color:#008080;">14</span> <span style="color:#000000;"> } </span><span style="color:#008080;">15</span> <span style="color:#0000ff;">else</span>{ <span style="color:#008000;">//</span><span style="color:#008000;"> 3 </span> <span style="color:#008080;">16</span> str = " 1 1"+"<br/>"+" 2 1"+"<br/>";<span style="color:#008000;">//</span><span style="color:#008000;"> </span> <span style="color:#008080;">17</span> $("#txt"<span style="color:#000000;">).html(str); </span><span style="color:#008080;">18</span> <span style="color:#0000ff;">for</span> (i=3;i<=obj;i++){ <span style="color:#008000;">//</span><span style="color:#008000;">for </span> <span style="color:#008080;">19</span> sum = a + b; <span style="color:#008000;">//</span><span style="color:#008000;"> </span> <span style="color:#008080;">20</span> a = b; <span style="color:#008000;">//</span><span style="color:#008000;"> b a</span> <span style="color:#008080;">21</span> b = sum; <span style="color:#008000;">//</span><span style="color:#008000;"> sum b</span> <span style="color:#008080;">22</span> str = " "+(i)+" "+sum+"<br/>"<span style="color:#000000;">; </span><span style="color:#008080;">23</span> $("#txt").append(str); <span style="color:#008000;">//</span><span style="color:#008000;"> id txt div 。</span> <span style="color:#008080;">24</span> <span style="color:#000000;"> } </span><span style="color:#008080;">25</span> <span style="color:#000000;"> } </span><span style="color:#008080;">26</span> <span style="color:#000000;"> } </span><span style="color:#008080;">27</span> rabbit(obj); <span style="color:#008000;">//</span><span style="color:#008000;"> ( input )</span> <span style="color:#008080;">28</span> <span style="color:#000000;">} </span><span style="color:#008080;">29</span>
 
転載先:https://www.cnblogs.com/lxgandlz/p/5098714.html