Groupon Noodles by Prepare:max Cycle Length

Source: Internet
Author: User

The topic is to encounter even/2, encounter odd number + 1 topics, and then find a range of all the numbers in Max cycle length. For a number, say 44, according to the formula of the problem is calculated, the process is 44, 22, 11, 8, 9, 1 (blind), from 44 to 1 of the length of this sequence, called cycle length. Then the title is given a range, such as [2,300], which asks for the maximum value of the cycle length of all the numbers. Follow up run 1 to 1 million

A number of a number is slowly calculated, the following algorithm to find the cycle length of each number

1  Packagesorting;2 ImportJava.util.*;3 4  Public classSolution2 {5      PublicList<integer> Maxcyclelen (intMinintmax) {6List<integer> maxcycle =NewArraylist<integer>();7          for(intI=min; i<=max; i++) {8 Helper (maxcycle, i);9         }Ten         returnmaxcycle; One     } A      -      Public voidHelper (list<integer> maxcycle,intnum) { -         intLen = 1; the          while(num! = 1) { -             if(num%2 = = 1) num = num*3+1; -             Elsenum = NUM/2; -len++; +         } - Maxcycle.add (len); +     } A  at     /** -      * @paramargs -      */ -      Public Static voidMain (string[] args) { -         //TODO auto-generated Method Stub -Solution2 Sol =NewSolution2 (); inlist<integer> res = Sol.maxcyclelen (1, 100000); - System.out.println (res); to     } +  -}

Follow up: Build a lookup table, count the numbers and don't forget it.

1  Packagesorting;2 ImportJava.util.*;3 4  Public classSolution3 {5Hashmap<integer, integer>map;6      PublicList<integer> Maxcyclelen (intMinintmax) {7List<integer> maxcycle =NewArraylist<integer>();8Map =NewHashmap<integer, integer>();9          for(intI=min; i<=max; i++) {Ten Helper (maxcycle, i); One         } A         returnmaxcycle; -     } -      the      Public voidHelper (list<integer> maxcycle,intnum) { -         intLen = 1; -         intNumcopy =num; -          while(num! = 1) { +             if(Map.containskey (num)) { -Len + = Map.get (num)-1; +                  Break; A             } at             if(num%2 = = 1) num = num*3+1; -             Elsenum = NUM/2; -len++; -         } - Maxcycle.add (len); - map.put (numcopy, Len); in     } -  to     /** +      * @paramargs -      */ the      Public Static voidMain (string[] args) { *         //TODO auto-generated Method Stub $Solution3 Sol =NewSolution3 ();Panax Notoginsenglist<integer> res = Sol.maxcyclelen (1, 100000); - System.out.println (res); the     } +  A}

Or

1      Public voidHelper (list<integer> maxcycle,intnum) {2         intLen = 0;3         intNumcopy =num;4          while(!Map.containskey (num)) {5             if(num = = 1) {6Map.put (1, 1);7Maxcycle.add (1);8                 return;9             }Ten             if(num%2 = = 1) num = num*3+1; One             Elsenum = NUM/2; Alen++; -         } -Len = len +map.get (num); the Maxcycle.add (len); - map.put (numcopy, Len); -}

Groupon Noodles by Prepare:max Cycle Length

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.