当前位置: 移动技术网 > IT编程>开发语言>Java > Java数据结构及算法实例:三角数字

Java数据结构及算法实例:三角数字

2019年07月22日  | 移动技术网IT编程  | 我要评论
/** 
 * 三角数字: 
 * 比达哥斯拉领导下的古希腊数学家发现了一个有趣的数字序列1, 3, 6, 10, 15, 21,... 
 * 你能看出他们有什么规律么? 
 * 对了它的规律就是f(x) = x+ f(x-1) 
 * 想想是不是很像小时候打算盘从1一直加到100啊 
 */ 
package al; 
public class triangle { 
  public static void main(string[] args) { 
    triangle triangle = new triangle(); 
    int result = triangle.getvalue(100); 
    system.out.println("result is " + result); 
  } 
  /** 
   * @param n 第n项 
   * @return 该项的三角数字值 
   */ 
  public int getvalue (int n) { 
    if (n == 1) { 
      return 1; 
    } else { 
      return n + getvalue(n - 1); 
    } 
  } 
} 


如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网