自己练习递归算法(示例)

//要注意:阶乘时int类型的长度,如果超出长度结果会为负数
1
//递归,100之间的数的和,从100开始 2 public static int one(int n) 3 { 4 if(n == 1) 5 { 6 return 1; 7 } 8 else 9 { 10 return n+one(n-1); 11 } 12 } 13 //递归,阶乘 14 public static int two(int a) 15 { 16 17 if(a ==1) 18 { 19 return 1; 20 } 21 else 22 { 23 return a * two(a-1); 24 } 25 }
 1 package javabase;
 2 
 3 public class AnimalTest {
 4     public static void main(String[] args) {
 5        
 6         //递归
 7         System.out.println("1-100之间的和是:"+one(100));
 8 
 9         System.out.println("阶乘商:"+two(5));
10 
11     }
12    
13 }

static 
我是代码搬运工!!!
原文地址:https://www.cnblogs.com/FanKL/p/13998783.html