Write java1,1,2,4,7,13,24,44 algorithm in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.