Write java1,1,2,4,7,13,24,44 algorithm in Java ---- Baidu know import java.util.arraylist;import java.util.list;import java.util.scanner;public class result {//first three numbers private int a = 1, b = 1, c = 2;private list<integer> list = null;//construction Method Result () {list = new ArrayList<Integer> (); List.add (a); List.add (b); List.add (c);} Returns the result Public int getresult (int index) {if (index >= 0 && index <=3) {return list.get (index);} else if (index < 0) {return -1;} Return calc (Index). get (index);} Calculated and added to the collection Private list<integer> calc (Int index) {for (int i=3; i<= index; i++) {//each number equals the sum of the first three int temp = list.get (i-1) + list.get (i-2) + list.get (i-3); List.add (temp);} Return list;} Public static void main (String[] args) {system.out.println ("Please enter location:"); Int index = new scanner ( system.in). Nextint (); SYSTEM.OUT.PRINTLN (The result of the " + index + " position is: " + new result (). GetResult (Index - 1));}} This type of article is used only as a record.
This article is from the It Traveler blog, so be sure to keep this source http://fylxopensource.blog.51cto.com/1328715/1543520
Write java1,1,2,4,7,13,24,44 algorithm in Java