How not cycle to calculate an accumulated 100

  As stated, first think about their own a minute.

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  ...

  60

  Well, one minute to now reveal the answer: recursion.

  Know the answer, the problem is solved, and no more than 10 lines of code:  

public  class the Recursion { 

    // recursive summation, if num is 100, calculated: 100 + 99 + 98 + ... +. 1 
    public  static  int multiplication ( int num) {
         IF (num>. 1 ) {
             return num + multiplication ( NUM -. 1 ); 
        } 
        return NUM; 
    } 

    public  static  void main (String [] args) 
    { 
        System.out.println (multiplication ( 100 )); 
    } 
}

  run:

5050

  

  

  

Guess you like

Origin www.cnblogs.com/wuxun1997/p/10971400.html