数据结构与算法(JAVA篇)之递归算法(一)
2009-01-20 21:59
344 查看
/**
*
* @author SunnyMoon
*/
/**
* 概念介绍:
* 递归是一种方法(函数)调用自已编程技术。
* 递归就是程序设计中的数学归纳法。
* 例如:tri(n)=1 if n=1
* tri(n)=n+tri(n-1) if n>1
* 可能while循环方法执行的速度比递归方法快,但是为什么采用递归呢。
* 采用递归,是因为它从概念上简化了问题,而不是因为它提高效率。
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Recursion {//求三角数字的递归算法:1,3,6,10,15,21, ......
static int theNumber;
public static void main(String[] args) throws IOException {
System.out.print("Enter a number: ");
theNumber = getInt();
//使用递归时调用,默认
int theAnswer = triangle(theNumber);
//使用非递归时调用
//int theAnswer=triangle2(theNumber);
System.out.println("Result: " + theAnswer);
}
public static int triangle(int n) {//递归方法,循环调用
if (n == 1) {
return 1;
} else {
return (n + triangle(n - 1));
}
}
public static int triangle2(int n) {//非递归方法
int total = 0;
while (n > 0) {
total = total + n--;
}
return total;
}
public static String getString() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
public static int getInt() throws IOException {
String s = getString();
return Integer.parseInt(s);
}
}
/**
* 运行结果:
* Enter a number:
* 3
* Result: 6
*/
/**
* 总结:
* 使用非递归的方式更简洁,更易懂,运行效率更高,为什么还要用递归的算法呢。
* 递归使规模逐渐降低的方式解决问题,以这种统一的方式解决足够复杂的问题。
*/
*
* @author SunnyMoon
*/
/**
* 概念介绍:
* 递归是一种方法(函数)调用自已编程技术。
* 递归就是程序设计中的数学归纳法。
* 例如:tri(n)=1 if n=1
* tri(n)=n+tri(n-1) if n>1
* 可能while循环方法执行的速度比递归方法快,但是为什么采用递归呢。
* 采用递归,是因为它从概念上简化了问题,而不是因为它提高效率。
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Recursion {//求三角数字的递归算法:1,3,6,10,15,21, ......
static int theNumber;
public static void main(String[] args) throws IOException {
System.out.print("Enter a number: ");
theNumber = getInt();
//使用递归时调用,默认
int theAnswer = triangle(theNumber);
//使用非递归时调用
//int theAnswer=triangle2(theNumber);
System.out.println("Result: " + theAnswer);
}
public static int triangle(int n) {//递归方法,循环调用
if (n == 1) {
return 1;
} else {
return (n + triangle(n - 1));
}
}
public static int triangle2(int n) {//非递归方法
int total = 0;
while (n > 0) {
total = total + n--;
}
return total;
}
public static String getString() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
public static int getInt() throws IOException {
String s = getString();
return Integer.parseInt(s);
}
}
/**
* 运行结果:
* Enter a number:
* 3
* Result: 6
*/
/**
* 总结:
* 使用非递归的方式更简洁,更易懂,运行效率更高,为什么还要用递归的算法呢。
* 递归使规模逐渐降低的方式解决问题,以这种统一的方式解决足够复杂的问题。
*/
相关文章推荐
- 数据结构与算法(JAVA篇)之递归算法(二)
- 数据结构与算法(JAVA篇)之递归算法(三)
- 数据结构与算法(JAVA篇)之递归算法(四)
- 数据结构与算法(Java描述)-16、递归算法及其应用
- Java数据结构与算法《十八》图
- 【java数据结构与算法学习】队列
- Java的递归算法
- java-递归算法1
- 使用递归算法结合数据库解析成java树形结构
- 数据结构与算法Java版——二叉排序树
- Java二分法(Binary Search)查找递归算法与迭代算法
- Java程序9 X 9乘法表(补:第二种方法:递归算法实现99乘法表|再补:解析递归算法)
- 用java实现:利用递归算法返转一个整数
- 用递归算法写一段程序实现 1+2+...+1000000 、java.lang.StackOverflowError 异常,原因,bug调查,处理
- 【JAVA基础】简析递归算法
- Java数据结构与算法(五)-双向链表
- 数据结构与算法(6)---Java语言实现:冒泡排序
- 只使用处理IO的printDigit函数,编写一个过程以输出任意实数可以为负(java数据结构与算法习题1.3)
- [置顶] 用递归算法得到Java的树形结构
- Java学习资料-Java常用算法-递归算法