用java语言写一个递归程序
时间: 2023-03-02 18:04:20 浏览: 70
好的,下面是一个使用Java语言编写的递归程序示例:
```java
public class RecursionExample {
public static void main(String[] args) {
int n = 5; // 定义递归的次数
recursion(n); // 调用递归方法
}
public static void recursion(int n) {
if (n > 0) {
System.out.println("递归层数:" + n);
recursion(n - 1); // 递归调用
}
}
}
```
这个程序使用递归来输出一个数字的递归次数,具体来说就是从数字5一直递减到1,每递减一次就输出当前递归的次数。程序首先定义了一个整数n表示递归的次数,然后调用了递归方法recursion()。递归方法中,首先判断递归次数是否大于0,如果是,则输出当前递归的次数,并且将n减1,然后再次调用递归方法recursion()。这样递归会一直执行到n等于0,递归过程中输出的数字就是递归的次数。
相关问题
java 写一个递归
### 回答1:
你可以使用 Java 的递归函数来写,例如:
public static int recursiveFunction(int n) {
if (n == 0) {
return 0;
}
else {
return n + recursiveFunction(n - 1);
}
}
### 回答2:
递归是一种编程技巧,指的是在函数内部调用自身的过程。下面是一个用Java语言写的递归实例:
递归求阶乘:
```java
public class RecursiveExample {
public static void main(String[] args) {
int n = 5;
int result = factorial(n);
System.out.println(n + "! = " + result);
}
public static int factorial(int n) {
if (n == 0 || n == 1) { // base case
return 1;
} else {
return n * factorial(n - 1); // recursion
}
}
}
```
在上述代码中,`factorial()` 方法用于计算给定数字的阶乘。当 `n` 为0或1时,表示递归已经到达最终结果,此时直接返回1(即基本情况)。否则,将 `n` 乘以 `factorial(n-1)` 的值,通过不断调用自身来推导出阶乘的结果(即递归情况)。
该程序中调用的 `factorial(5)` 实际上会依次调用 `factorial(4)`、`factorial(3)`、`factorial(2)`、`factorial(1)` 和 `factorial(0)`,从而逐步推导出5的阶乘。
递归在解决一些问题时能够提供简洁、优雅的解决方案,但需要注意避免进入无限递归的情况,同时要确保有明确定义的基本情况来终止递归推导。
### 回答3:
递归是一个算法或者方法调用自身的方式,实现递归可以写一个简单的例子来说明。
假设我们要编写一个递归函数,来计算一个正整数的阶乘。阶乘指的是将该正整数与比它小的所有正整数相乘,直到乘到1为止。
下面是用Java编写的递归函数来计算阶乘的例子:
```java
public class FactorialCalculator {
public static void main(String[] args) {
int number = 5;
long factorial = calculateFactorial(number);
System.out.println(number + "的阶乘是:" + factorial);
}
public static long calculateFactorial(int number) {
// 递归终止条件:当number等于1时,阶乘为1
if (number == 1) {
return 1;
}
// 递归调用:计算number的阶乘需要先计算(number-1)的阶乘,然后乘以number
return number * calculateFactorial(number - 1);
}
}
```
在上述代码中,我们定义了一个静态方法`calculateFactorial()`,接收一个整数作为参数。在方法中,我们首先设置递归终止条件,即当输入的数字为1时,直接返回1。然后通过递归调用自身,将问题转化为计算`(number-1)`的阶乘并与当前的number相乘。最终返回计算结果。
以上就是一个简单的Java递归函数的例子,通过递归调用的方式实现了计算阶乘的功能。
用java语言写一个全排列
### 回答1:
用java语言写一个全排列的程序可以这样:
public class Permutation{
public static void main(String[] args) {
int[] arr = {1, 2, 3};
permutation(arr, 0, arr.length-1);
}
public static void permutation(int[] arr, int start, int end){
if(start == end){
for(int i=0; i<=end; i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}else{
for(int i=start; i<=end; i++){
swap(arr, i, start);
permutation(arr, start+1, end);
swap(arr, i, start);
}
}
}
public static void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
### 回答2:
全排列是一种将一组元素进行全面排序的方法。在Java语言中,可以使用递归来实现全排列的算法。
首先,定义一个函数用于交换数组中两个下标位置的元素,以实现元素位置的变换。例如:
```java
private static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
```
然后,定义一个递归函数用于生成全排列。该函数需要传入待排列的数组、当前递归深度和数组长度。例如:
```java
private static void permute(int[] array, int depth, int length) {
if (depth == length - 1) {
// 递归终止条件:当递归深度等于数组长度减一时,输出当前排列
System.out.println(Arrays.toString(array));
} else {
// 从当前递归深度开始,将每个元素与后面的元素进行交换,并递归调用全排列函数
for (int i = depth; i < length; i++) {
swap(array, i, depth);
permute(array, depth + 1, length);
swap(array, i, depth);
}
}
}
```
最后,在主函数中调用全排列函数并传入待排列的数组。例如:
```java
public static void main(String[] args) {
int[] array = {1, 2, 3};
permute(array, 0, array.length);
}
```
运行程序,即可输出全排列的结果:
```
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 2, 1]
[3, 1, 2]
```
这样就完成了用Java语言编写全排列的程序。递归是实现全排列的常用方法,可以通过交换数组元素的位置来生成不同的排列组合。
### 回答3:
全排列是指给定一组数字或字符,通过不同的排列组合方式,将所有可能的结果都列出来。下面是一种使用Java语言实现全排列算法的方法:
```java
import java.util.ArrayList;
import java.util.List;
public class Permutation {
public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
if(nums == null || nums.length == 0) {
return result;
}
List<Integer> permutation = new ArrayList<>();
boolean[] used = new boolean[nums.length];
permuteHelper(nums, permutation, used, result);
return result;
}
private static void permuteHelper(int[] nums, List<Integer> permutation, boolean[] used, List<List<Integer>> result) {
if(permutation.size() == nums.length) {
result.add(new ArrayList<>(permutation));
return;
}
for(int i = 0; i < nums.length; i++) {
if(used[i]) {
continue;
}
permutation.add(nums[i]);
used[i] = true;
permuteHelper(nums, permutation, used, result);
permutation.remove(permutation.size() - 1);
used[i] = false;
}
}
public static void main(String[] args) {
int[] nums = {1, 2, 3};
List<List<Integer>> permutations = permute(nums);
for(List<Integer> permutation : permutations) {
System.out.println(permutation);
}
}
}
```
该算法使用递归的方式生成全排列结果。首先,创建一个辅助函数`permuteHelper`来实现实际的排列过程。在`permuteHelper`中,通过一个`permutation`列表来存储当前已排列的数字,使用一个`used`数组来标记数字是否已经被使用。
递归过程中,首先判断当前排列是否已经包含了所有的数字,如果是,则将当前排列添加到结果列表中。否则,遍历所有未被使用的数字,将其添加到当前排列中,并将其标记为已使用。然后递归调用`permuteHelper`继续生成下一个数的排列。完成递归后,需要将添加的数字从当前排列中移除,并将其标记为未使用,以便进行下一轮的遍历。
在`main`函数中,可以通过调用`permute`方法,并传入一个数组来获得该数组的全排列结果。最后,将结果列表打印出来,即可看到所有的全排列结果。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)