| 
 
 上楼梯每次只准一步或者两步,有多少走法? 
 
public class Go { 
 public static int i = 0; 
 public static void main(String[] args){ 
  calc("", 5); 
  System.out.println("总共有"+i+"种走法~"); 
 } 
  
 //上楼梯每次只准一步或者两步,有多少走法 
 public static void calc(String log, int num){ 
  if (num == 0) { 
   i++; 
   System.out.println(log.substring(0,log.length()-1)); 
   return; 
  }else if(num == 1) { 
   i++; 
   System.out.println(log+"1"); 
   return; 
  } 
  calc(log+"1,", num - 1); 
  calc(log+"2,", num - 2); 
 } 
}运行结果: 
                       
C:\w1>java Go 
1,1,1,1,1 
1,1,1,2 
1,1,2,1 
1,2,1,1 
1,2,2 
2,1,1,1 
2,1,2 
2,2,1 
总共有8种走法~ |