package com.java.recursion;
/**
* @描述 三角数据
* 1 3 6 10
*
* 1 1 1 1
* 1 1 1 1 1 1
* 1 1 1 1 1 1
* 1 1 1 1
*
*
* @项目名称 Java_DataStruct
* @包名 com.java.recursion
* @类名 Triangle
* @author chenlin
* @date 2012年6月17日 下午8:15:42
* @version 1.0
*/
public class Triangle {
/**
* 叠加法
* @param n
* @return
*/
public static int getTotal(int n){
int total = 0;
while(n > 0){
total += n;
n --;
}
return total;
}
/**
* 使用递归
* @param n
* @return
*/
public static int getTotalByRecusion(int n){
if (n == 1) {
return 1;
}else {
return getTotalByRecusion(n-1)+n;
}
}
public static void main(String[] args) {
System.out.println(getTotal(1));
System.out.println(getTotal(2));
System.out.println(getTotal(3));
System.out.println(getTotal(4));
System.out.println("递归========================");
System.out.println(getTotalByRecusion(1));
System.out.println(getTotalByRecusion(2));
System.out.println(getTotalByRecusion(3));
System.out.println(getTotalByRecusion(4));
}
}