本文目錄一覽:
java用遞歸算法求 1-2+3-4+5-6……+
思路:先用遞歸求出一個數的階乘,接着for循環累加求和。參考代碼:
#include
int
fun(int
n){
if(n==1)
return
1;//遞歸結束條件
return
n*fun(n-1);//遞歸式
}
int
main()
{
int
sum=0,i;
for(i=1;i=6;i++)//for循環累加求和
sum+=fun(i);
printf(“%d\n”,sum);
return
0;
}
/*
運行結果:
873
*/
評論
加載更多
java遞歸算法
1.漢諾塔問題
import javax.swing.JOptionPane;
public class Hanoi {
private static final String DISK_B = “diskB”;
private static final String DISK_C = “diskC”;
private static final String DISK_A = “diskA”;
static String from=DISK_A;
static String to=DISK_C;
static String mid=DISK_B;
public static void main(String[] args) {
String input=JOptionPane.showInputDialog(“please input the number of the disks you want me move.”);
int num=Integer.parseInt(input);
move(num,from,mid,to);
}
private static void move(int num, String from2, String mid2, String to2) {
if(num==1){
System.out.println(“move disk 1 from “+from2+” to “+to2);
}
else {
move(num-1,from2,to2,mid2);
System.out.println(“move disk “+num+” from “+from2+” to “+to2);
move(num-1,mid2,from2,to2);
}
}
}
2. 這是一個排列的例子,它所做的工作是將輸入的一個字符串中的所有元素進行排序並輸出,例如:你給出的參數是”abc” 則程序會輸出:
abc
acb
bac
bca
cab
cba
(1)算法的出口在於:low=high也就是現在給出的排列元素只有一個時。
(2)算法的逼近過程:先確定排列的第一位元素,也就是循環中i所代表的元素,
然後low+1開始減少排列元素,如此下去,直到low=high
public static void permute(String str) {
char[] strArray = str.toCharArray();
permute(strArray, 0, strArray.length – 1);
}
public static void permute(char[] list, int low, int high) {
int i;
if (low == high) {
String cout = “”;
for (i = 0; i = high; i++)
cout += list[i];
System.out.println(cout);
} else {
for (i = low; i = high; i++) {
char temp = list[low];
list[low] = list[i];
list[i] = temp;
permute(list, low + 1, high);
temp = list[low];
list[low] = list[i];
list[i] = temp;
}
}
}
3。這是一個組合的例子,與上述的例子相似,只是它所做的工作是,輸出所給字符串中制定數目的元素的組合種類
(1)程序出口在於n=1,此時只要輸出目標數組的所有元素即可
(2)逼近過程,當n1 的時候,我們先取第一個元素放入目標數組中,然後n-1,如此下去,最後出來。
import javax.swing.JOptionPane;
public class Combination {
/**
* @param args
*/
public static void main(String[] args) {
String input = JOptionPane.showInputDialog(“please input your String: “);
String numString = JOptionPane.showInputDialog(“please input the number of your Combination: “);
int num = Integer.parseInt(numString);
Combine(input, num);
}
private static void Combine(String input, int num) {
char[] a = input.toCharArray();
String b = “”;
Combine(a, num, b, 0, a.length);
}
private static void Combine(char[] a, int num, String b, int low, int high) {
if (num == 0) {
System.out.println(b);
} else {
for (int i = low; i a.length; i++) {
b += a[i];
Combine(a, num – 1, b, i+1, a.length);
b=b.substring(0, b.length()-1);
}
}
}
}
用java遞歸算法求一個數字的階乘
1、採用自頂向上的遞歸方法,代碼如下:
import java.util.Scanner;
public class Test {
@SuppressWarnings(“resource”)
public static void main(String[] args) {
// 從控制台輸入一個整數
Scanner in = new Scanner(System.in);
int b = in.nextInt();
// 聲明一個Test對象,調用cal方法獲得結果
Test test = new Test();
long a = test.cal(b);
System.out.println(a);
}
// 通過遞歸掉調用最終返回結果
public long cal(int number) {
// 如果數字為1,則直接返回
if (number == 1) {
return 1;
} else {// 否則遞歸求值
return number * cal(number – 1);
}
}
}
2、遞歸方法:
遞歸算法是把問題轉化為規模縮小了的同類問題的子問題。然後遞歸調用函數(或過程)來表示問題的解。一個過程(或函數)直接或間接調用自己本身,這種過程(或函數)叫遞歸過程(或函數).
3、特點:
(1) 遞歸就是在過程或函數里調用自身。
(2) 在使用遞歸策略時,必須有一個明確的遞歸結束條件,稱為遞歸出口。
(3) 遞歸算法解題通常顯得很簡潔,但遞歸算法解題的運行效率較低。所以一般不提倡用遞歸算法設計程序。
(4) 在遞歸調用的過程當中系統為每一層的返回點、局部量等開闢了棧來存儲。遞歸次數過多容易造成棧溢出等。所以一般不提倡用遞歸算法設計程序。
JAVA中的遞歸方法,求講一下。
方法遞歸和循環語句差不多,打個比喻。方法遞歸是小明上樓拿東西,一樓,二樓,三樓……樓頂。在樓頂拿到想要的東西以後,你總不能直接跳下來吧。你得一層一層的返回下來。循環就是驢拉磨,你轉多少圈都是在原地。變化的只是盤子里的東西有變化。方法遞歸不會進入死循環,但陷的太深系統會崩潰。
答得不好抱歉
用java遞歸方法實現
1、遞歸做為一種算法在程序設計語言中廣泛使用,是指函數/過程/子程序在運行過程中直接或間接調用自身而產生的重入現象。
2、遞歸算法一般用於解決三類問題:
1)數據的定義是按遞歸定義的。(Fibonacci(斐波那契)的函數)
2)問題解法按遞歸算法實現。(回溯)
3)數據的結構形式是按遞歸定義的。(樹的遍歷,圖的搜索)
原創文章,作者:OKMJ,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/133739.html