import java.util.Scanner;
創(chuàng)新互聯(lián)基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺(tái)為眾多戶提供綿陽(yáng)電信機(jī)房機(jī)柜租用 四川大帶寬租用 成都機(jī)柜租用 成都服務(wù)器租用。
class Example
{
/*遞歸方法*/
public long square(int n)
{
if (0 == n)//遞歸的出口
{
return 0;
}
else
{
return square(n-1) + 2*n - 1;
}
}
/*輸入數(shù)據(jù)n的方法*/
public int scan()
{
int number = 0;
Scanner scan = null;
while(true)
{
System.out.print("請(qǐng)輸入 n 的值:");
try{
scan = new Scanner(System.in);
number = scan.nextInt();
break;
}catch(Exception e)
{
System.out.println("你輸入的數(shù)據(jù)n 錯(cuò)誤!");
System.out.println("請(qǐng)重新輸入...");
}
}
return number;
}
public boolean isContinue ()
{
String src = null;
Scanner scan = null;
while(true){
System.out.print("是否還需要繼續(xù)計(jì)算?y(yes)/n(no) : ");
scan = new Scanner(System.in);
src = scan.nextLine();
if(src.equalsIgnoreCase("y") || src.equalsIgnoreCase("yes"))
{
return true;
}
else if(src.equalsIgnoreCase("n") || src.equalsIgnoreCase("no"))
{
return false;
}
}
}
public static void main(String[] args)
{
int number = 0;
Example exam = null;
do{
exam = new Example();
number = exam.scan();
long result = exam.square(number);
System.out.println("square(" + number + ") = " + result);
}while(exam.isContinue());
}
}
這個(gè)很好寫的,代碼如下:
private ListDept recursionDept(ListDept ld){
for(int i=0; ild.size(); i++) {
Dept d = ld.get(i)
loop(d);
}
}
private void loop(Dept d) {
ListDept children=service.getChildDept(d.id);
if (children.size() 0) {
d.setChildren(children); // 這里假設(shè)子列表屬性的名字就是children
for(int j=0; jchidren.size(); j++){
loop(children.get(j);
}
}
}
這個(gè)題目對(duì)初學(xué)者來說比較難的一點(diǎn)是,得想明白要自己建一個(gè)遞歸方法(loop)
階乘:
要求:給定一個(gè)數(shù)值,計(jì)算出它的階乘值,例如5的階乘為5*4*3*2*1
實(shí)現(xiàn):
[html] view plaincopy
span style="font-size:12px;" ?// 利用遞歸實(shí)現(xiàn)一個(gè)數(shù)的階乘值 ? ? ?private static BigDecimal getNum(BigDecimal inNum) { ? ? ? ? ?if (inNum.compareTo(BigDecimal.ONE) == 0) { ? ? ? ? ? ? ?return inNum; ? ? ? ? ?} ? ? ? ? ?return inNum.multiply(getNum(inNum.subtract(BigDecimal.ONE))); ? ? ?}/span
(2)Fibonacci數(shù)列:1,1,2,3,5,8,13……
要求:找出數(shù)列中指定index位置的數(shù)值
實(shí)現(xiàn):
[html] view plaincopy
span style="font-size:12px;" ?// 利用遞歸實(shí)現(xiàn)了Fibonacci數(shù)列 ? ? ?private static int fab(int index) { ? ? ? ? ?if (index == 1 || index == 2) { ? ? ? ? ? ? ?return 1; ? ? ? ? ?} else { ? ? ? ? ? ? ?return fab(index - 1) + fab(index - 2); ? ? ? ? ?} ? ? ?}/span
(3)漢諾塔
要求:漢諾塔挪動(dòng)
實(shí)現(xiàn):
[html] view plaincopy
span style="font-size:12px;" ?span style="white-space:pre;" /spanprivate static final String DISK_B = "diskB"; ? ?span style="white-space:pre;" ? /spanprivate static final String DISK_C = "diskC"; ? ?span style="white-space:pre;" ? /spanprivate static final String DISK_A = "diskA"; ? ?span style="white-space:pre;" ? /spanstatic String from=DISK_A; ?span style="white-space:pre;" /span ?static String to=DISK_C; ?span style="white-space:pre;" /span ?static String mid=DISK_B; ? ?span style="white-space:pre;" /span ?public static void main(String[] args) { ?span style="white-space:pre;" /span ? ? ?String input=JOptionPane.showInputDialog("please input the number of the disks you want me move."); ?span style="white-space:pre;" /span ? ? ?int num=Integer.parseInt(input); ?span style="white-space:pre;" /span ? ? ?move(num,from,mid,to); ?span style="white-space:pre;" /span ?}/span
[html] view plaincopy
span style="font-size:12px;" ?// 利用遞歸實(shí)現(xiàn)漢諾塔 ? ? ?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); ? ? ? ? ?} ? ? ?}/span
(4)排列組合
要求:將輸入的一個(gè)字符串中的所有元素進(jìn)行排序并輸出,例如:你給出的參數(shù)是"abc",
則程序會(huì)輸出
abc
acb
bac
bca
cab
cba
實(shí)現(xiàn):
[html] view plaincopy
span style="font-size:12px;"span style="white-space:pre;" ? /spanpublic static void permute(String str) { ? span style="white-space:pre;" ? ?/span ? char[] strArray = str.toCharArray(); ? ?span style="white-space:pre;" ? /span permute(strArray, 0, strArray.length - 1); ?span style="white-space:pre;" /span}/span
[html] view plaincopy
span style="font-size:12px;" ?// 利用遞歸實(shí)現(xiàn),將輸入的一個(gè)字符串中的所有元素進(jìn)行排序并輸出 ? ? ?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];
代碼如下:
import java.util.ArrayList;
import java.util.List;
class Org {
private String id;
private String name;
private String pid;
public Org(String id, String name, String pid) {
this.id = id;
this.name = name;
this.pid = pid;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
@Override
public String toString() {
return "Org [id=" + id + ", name=" + name + ", pid=" + pid + "]";
}
}
public class App {
static void find(ListOrg list, String pid) {
list.stream().filter(p - p.getPid().equals(pid))
.forEach(org - {
System.out.println(org);
find(list, org.getId());
});
}
public static void main(String[] args) {
ListOrg list = new ArrayList();
list.add(new Org("111", "公司", "0"));
list.add(new Org("222", "部門", "111"));
list.add(new Org("333", "小組", "222"));
list.add(new Org("444", "員工1", "333"));
list.add(new Org("555", "員工2", "333"));
find(list, "0");
System.out.println("------------------------------------");
find(list, "111");
}
}
運(yùn)行結(jié)果:
下面遞歸寫了一段遞歸累加到100,每加20個(gè)就換行輸出。
package?zhidao;
public?class?Digui?{
public?static?int?add(int?num){
int?sum?=?0;
StringBuffer?sb?=?new?StringBuffer();
if?(num?=?0)?{
return?0;
}else{
if?(num?==?1)?{
sum?=?sum+1;
}else?{
sum?=?add(num-1)+num;
}
if?(num?%?20?==?0)?{
System.out.println("[index?=?"+num+"?sum?=?"+sum+"]");
}else?{
System.out.print("[index?=?"+num+"?sum?=?"+sum+"],");
}
}
return?sum;
}
public?static?void?main(String[]?args)?{
add(100);
}
}