Import java.io.bufferedreader;import JAVA.IO.INPUTSTREAMREADER;//1,3,6,10,15...N triangle */* #1 * # #1 +2 * # # #1 +2+3 * # # # #1 +2+ 3+4 * # # # # # # #1 +2+3+4+5 * ... The 1th layer is 1, the nth layer equals n + (f (n-1)) */public class Trianglenumber {static int triangle (int n) {if (n < 1) return 0;if (n = = 1) r Eturn 1;return n + triangle (n-1); Recursive to the inner layer, returns the result from the bottom recursive upward and evaluates with N and}//if the recursive invocation of a function appears at the end of the function, it is called the tail recursive function static int triangle (int n, int last) {//last initial 0-tailed recursive if ( N < 1) return 0;if (n = = 1) return 1 + last;return triangle (n-1, n + last); Recursively to the inner layer, the bottom level directly calculates the final result and returns,}//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);} Stack save processing steps. Here is the value for each step of the save calculation process private static class Mystack {int maxsize;int[] stackary;int top;//index of top of stack mystack (int max) {This.max Size = 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;}} Use the stack to implement the 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, to calculate the number of trigonometric values, enter the number of digits ( Enter exit End 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, to calculate the number of trigonometric values, please enter the number of digits (enter exit to end the program)-----"); line = Br.readline (); SYSTEM.OUT.PRINTLN ("-----Program exits-----"); Runtime.getruntime (). exit (0);}}
Test
-----program starts, to calculate the number of trigonometric values, enter the number of digits (enter exit to end the program)-----10 recursion: 55 Tail Recursion: 55 non-recursive: 55 Stack implementation: In the beginning of the-----program, to calculate the number of trigonometric values, enter the number of digits ( Enter exit end program)-----100 recursion: 5050 Tail Recursion: 5050 Non-recursive: 5050 Stack implementation: 5050-----program, to calculate the number of trigonometric values, enter the number of digits (enter exit to end the program)----- Exit-----Program Exits-----
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java recursive, tail recursive, non-recursive, stack processing trigonometric number problem