Java recursion, tail recursion, non-recursion, and stack processing triangle number

Source: Internet
Author: User

Java recursion, tail recursion, non-recursion, and stack processing triangle number

 

 

Import java. io. bufferedReader; import java. io. inputStreamReader; // 1, 3, 6, 10, 15... n triangle number/** #1 * ## 1 + 2 * ### 1 + 2 + 3 * #### 1 + 2 + 3 + 4 *#### #1 + 2 + 3 + 4 + 5 *... the first layer is 1, and the nth layer is equal to n + (f (n-1) */public class TriangleNumber {static int triangle (int n) {if (n <1) return 0; if (n = 1) return 1; return n + triangle (n-1); // recursion to layer-4 calculation, returns the result recursively from the bottom layer and calculates the result with n and} // If a recursive call of a function appears at the end of the function, it is called the static int triangle (int n, int last) {// The last is initially passed 0 tail recursion if (n <1) return 0; if (n = 1) return 1 + last; return triangle (n-1, n + last); // recursively calculates the final result and returns the result at the bottom layer.} // non-recursive static int triangle2 (int n) {int sum = 0; for (int I = 1; I <= n; I ++) {sum + = I;} return sum;} static int str2Int (String num) {return Integer. valueOf (num);} // Save the stack. here is the value of each step in the computing process: private static class MyStack {int maxSize; int [] stackAry; int top; // index on the top of the stack MyStack (int max) {This. maxSize = max; this. stackAry = new int [maxSize]; top =-1;} void push (int n) {stackAry [++ top] = n;} int pop () {return stackAry [top --];} int peek () {return stackAry [top];} boolean isEmpty () {return top =-1 ;}} // implement static int triangle3 (int n) {MyStack stack = new MyStack (n); int result = 0; while (n> 0) {stack. push (n); n --;} while (! Stack. isEmpty () {int temp = stack. pop (); result + = temp;} return result;} public static void main (String [] args) throws Exception {System. out. println (----- program start. to calculate the maximum number of triangles, enter the number of digits (enter exit program) -----); BufferedReader br = new BufferedReader (new InputStreamReader (System. in); String line = br. readLine (); while (! Line. equals (exit) {int n = str2Int (line); System. out. println (recursion: + triangle (n); System. out. println (tail recursion: + triangle (n, 0); System. out. println (non-recursive: + triangle2 (n); System. out. println (stack implementation: + triangle3 (n); System. out. println (); System. out. println (----- program start. to calculate the maximum number of triangles, enter the number of digits (enter exit program) -----); line = br. readLine ();} System. out. println (----- program exit -----); Runtime. getRuntime (). exit (0 );}}

Test

 

 

----- The program starts. to calculate the number of triangle values, enter the number of digits (enter the exit program) ----- 10 recursion: 55 tail recursion: 55 non recursion: 55 stack implementation: 55 ----- the program starts. to calculate the maximum number of triangle values, enter the number of digits (enter the exit program) ----- 100 recursion: 5050 tail recursion: 5050 non recursion: 5050 stack implementation: 5050 ----- program start. to calculate the maximum number of triangles, enter the number of digits (enter exit program) ----- exit ----- program exit -----


 

 

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.