Algorithm: Climb Stairs

Question:

If you areonly able toclimb some stairs with 1,2 or 3 steps in each time,how many possibilities you have when youclimb10 stairs? Pleasedisplay all thepossibilities.

Codes:

package com.test; public class ClimbStairs { static int count = 0; static int index = 0; static int step[] = new int[10]; public static void outputStep() { int i; for (i = 0; i <index; i++) System.out.print(step[i]); System.out.println(); } public static void climb(int n) { if (n == 0) { count++; System.out.println("---------the NO." + count + "----------"); outputStep(); return; } step[index++] = 1; climb(n-1); --index; if (n > 1) { step[index++] = 2; climb(n-2); --index; } if (n > 2) { step[index++] = 3; climb(n-3); --index; } } /** * @param args */ public static void main(String[] args) { climb(10); } }

Analysis:

1. We need a array to store each count of step we move. Obviously, the max ofthe array size should be the length of stairs. In my example, the array is step[].

2. We also need a var to mark the position of step[] so that we can know what time we have moved.

3. Var Count is used torecord how many possibilities we have got in totally.

4. Now seefunc climb(N):

Suppose that we climbed 1 step in first time. Then, we can store 1 to the step[0]. Obviously, index is 0 at here. After that we still haveN-1 steps need to climb. So, we have to invoke recursion which is stated as climb(N-1) to repeat these operations.

However,what if we want to climb 2 steps or 3 steps in first time? In order to travel all the possibilities, we have to go back to previous step and re-climb with the number of newstep. Base on this idea, we canmakeindex=index-1 to go back to previous step and store 2or 3 to step[index]and keep index moving with index++. Likewise, we need to invoke climb(N-1) to climb the rest of steps.

At last, when the steps we have climbed is 0, that means wehave already finished toclimb. Sowe can stop recursion invoking and output array step to display one of Moving Solution. Since this is a recursion calling, wewill get all the possibilities.

Result:

---------the NO.1---------- 1111111111 ---------the NO.2---------- 111111112 ---------the NO.3---------- 111111121 ---------the NO.4---------- 11111113 ---------the NO.5---------- 111111211 ---------the NO.6---------- 11111122 ---------the NO.7---------- 11111131 ---------the NO.8---------- 111112111 ---------the NO.9---------- 11111212 ---------the NO.10---------- 11111221 ---------the NO.11---------- 1111123 ---------the NO.12---------- 11111311 ---------the NO.13---------- 1111132 ---------the NO.14---------- 111121111 ---------the NO.15---------- 11112112 ---------the NO.16---------- 11112121 ---------the NO.17---------- 1111213 ---------the NO.18---------- 11112211 ---------the NO.19---------- 1111222 ---------the NO.20---------- 1111231 ---------the NO.21---------- 11113111 ---------the NO.22---------- 1111312 ---------the NO.23---------- 1111321 ---------the NO.24---------- 111133 ---------the NO.25---------- 111211111 ---------the NO.26---------- 11121112 ---------the NO.27---------- 11121121 ---------the NO.28---------- 1112113 ---------the NO.29---------- 11121211 ---------the NO.30---------- 1112122 ---------the NO.31---------- 1112131 ---------the NO.32---------- 11122111 ---------the NO.33---------- 1112212 ---------the NO.34---------- 1112221 ---------the NO.35---------- 111223 ---------the NO.36---------- 1112311 ---------the NO.37---------- 111232 ---------the NO.38---------- 11131111 ---------the NO.39---------- 1113112 ---------the NO.40---------- 1113121 ---------the NO.41---------- 111313 ---------the NO.42---------- 1113211 ---------the NO.43---------- 111322 ---------the NO.44---------- 111331 ---------the NO.45---------- 112111111 ---------the NO.46---------- 11211112 ---------the NO.47---------- 11211121 ---------the NO.48---------- 1121113 ---------the NO.49---------- 11211211 ---------the NO.50---------- 1121122 ---------the NO.51---------- 1121131 ---------the NO.52---------- 11212111 ---------the NO.53---------- 1121212 ---------the NO.54---------- 1121221 ---------the NO.55---------- 112123 ---------the NO.56---------- 1121311 ---------the NO.57---------- 112132 ---------the NO.58---------- 11221111 ---------the NO.59---------- 1122112 ---------the NO.60---------- 1122121 ---------the NO.61---------- 112213 ---------the NO.62---------- 1122211 ---------the NO.63---------- 112222 ---------the NO.64---------- 112231 ---------the NO.65---------- 1123111 ---------the NO.66---------- 112312 ---------the NO.67---------- 112321 ---------the NO.68---------- 11233 ---------the NO.69---------- 11311111 ---------the NO.70---------- 1131112 ---------the NO.71---------- 1131121 ---------the NO.72---------- 113113 ---------the NO.73---------- 1131211 ---------the NO.74---------- 113122 ---------the NO.75---------- 113131 ---------the NO.76---------- 1132111 ---------the NO.77---------- 113212 ---------the NO.78---------- 113221 ---------the NO.79---------- 11323 ---------the NO.80---------- 113311 ---------the NO.81---------- 11332 ---------the NO.82---------- 121111111 ---------the NO.83---------- 12111112 ---------the NO.84---------- 12111121 ---------the NO.85---------- 1211113 ---------the NO.86---------- 12111211 ---------the NO.87---------- 1211122 ---------the NO.88---------- 1211131 ---------the NO.89---------- 12112111 ---------the NO.90---------- 1211212 ---------the NO.91---------- 1211221 ---------the NO.92---------- 121123 ---------the NO.93---------- 1211311 ---------the NO.94---------- 121132 ---------the NO.95---------- 12121111 ---------the NO.96---------- 1212112 ---------the NO.97---------- 1212121 ---------the NO.98---------- 121213 ---------the NO.99---------- 1212211 ---------the NO.100---------- 121222 ---------the NO.101---------- 121231 ---------the NO.102---------- 1213111 ---------the NO.103---------- 121312 ---------the NO.104---------- 121321 ---------the NO.105---------- 12133 ---------the NO.106---------- 12211111 ---------the NO.107---------- 1221112 ---------the NO.108---------- 1221121 ---------the NO.109---------- 122113 ---------the NO.110---------- 1221211 ---------the NO.111---------- 122122 ---------the NO.112---------- 122131 ---------the NO.113---------- 1222111 ---------the NO.114---------- 122212 ---------the NO.115---------- 122221 ---------the NO.116---------- 12223 ---------the NO.117---------- 122311 ---------the NO.118---------- 12232 ---------the NO.119---------- 1231111 ---------the NO.120---------- 123112 ---------the NO.121---------- 123121 ---------the NO.122---------- 12313 ---------the NO.123---------- 123211 ---------the NO.124---------- 12322 ---------the NO.125---------- 12331 ---------the NO.126---------- 13111111 ---------the NO.127---------- 1311112 ---------the NO.128---------- 1311121 ---------the NO.129---------- 131113 ---------the NO.130---------- 1311211 ---------the NO.131---------- 131122 ---------the NO.132---------- 131131 ---------the NO.133---------- 1312111 ---------the NO.134---------- 131212 ---------the NO.135---------- 131221 ---------the NO.136---------- 13123 ---------the NO.137---------- 131311 ---------the NO.138---------- 13132 ---------the NO.139---------- 1321111 ---------the NO.140---------- 132112 ---------the NO.141---------- 132121 ---------the NO.142---------- 13213 ---------the NO.143---------- 132211 ---------the NO.144---------- 13222 ---------the NO.145---------- 13231 ---------the NO.146---------- 133111 ---------the NO.147---------- 13312 ---------the NO.148---------- 13321 ---------the NO.149---------- 1333 ---------the NO.150---------- 211111111 ---------the NO.151---------- 21111112 ---------the NO.152---------- 21111121 ---------the NO.153---------- 2111113 ---------the NO.154---------- 21111211 ---------the NO.155---------- 2111122 ---------the NO.156---------- 2111131 ---------the NO.157---------- 21112111 ---------the NO.158---------- 2111212 ---------the NO.159---------- 2111221 ---------the NO.160---------- 211123 ---------the NO.161---------- 2111311 ---------the NO.162---------- 211132 ---------the NO.163---------- 21121111 ---------the NO.164---------- 2112112 ---------the NO.165---------- 2112121 ---------the NO.166---------- 211213 ---------the NO.167---------- 2112211 ---------the NO.168---------- 211222 ---------the NO.169---------- 211231 ---------the NO.170---------- 2113111 ---------the NO.171---------- 211312 ---------the NO.172---------- 211321 ---------the NO.173---------- 21133 ---------the NO.174---------- 21211111 ---------the NO.175---------- 2121112 ---------the NO.176---------- 2121121 ---------the NO.177---------- 212113 ---------the NO.178---------- 2121211 ---------the NO.179---------- 212122 ---------the NO.180---------- 212131 ---------the NO.181---------- 2122111 ---------the NO.182---------- 212212 ---------the NO.183---------- 212221 ---------the NO.184---------- 21223 ---------the NO.185---------- 212311 ---------the NO.186---------- 21232 ---------the NO.187---------- 2131111 ---------the NO.188---------- 213112 ---------the NO.189---------- 213121 ---------the NO.190---------- 21313 ---------the NO.191---------- 213211 ---------the NO.192---------- 21322 ---------the NO.193---------- 21331 ---------the NO.194---------- 22111111 ---------the NO.195---------- 2211112 ---------the NO.196---------- 2211121 ---------the NO.197---------- 221113 ---------the NO.198---------- 2211211 ---------the NO.199---------- 221122 ---------the NO.200---------- 221131 ---------the NO.201---------- 2212111 ---------the NO.202---------- 221212 ---------the NO.203---------- 221221 ---------the NO.204---------- 22123 ---------the NO.205---------- 221311 ---------the NO.206---------- 22132 ---------the NO.207---------- 2221111 ---------the NO.208---------- 222112 ---------the NO.209---------- 222121 ---------the NO.210---------- 22213 ---------the NO.211---------- 222211 ---------the NO.212---------- 22222 ---------the NO.213---------- 22231 ---------the NO.214---------- 223111 ---------the NO.215---------- 22312 ---------the NO.216---------- 22321 ---------the NO.217---------- 2233 ---------the NO.218---------- 2311111 ---------the NO.219---------- 231112 ---------the NO.220---------- 231121 ---------the NO.221---------- 23113 ---------the NO.222---------- 231211 ---------the NO.223---------- 23122 ---------the NO.224---------- 23131 ---------the NO.225---------- 232111 ---------the NO.226---------- 23212 ---------the NO.227---------- 23221 ---------the NO.228---------- 2323 ---------the NO.229---------- 23311 ---------the NO.230---------- 2332 ---------the NO.231---------- 31111111 ---------the NO.232---------- 3111112 ---------the NO.233---------- 3111121 ---------the NO.234---------- 311113 ---------the NO.235---------- 3111211 ---------the NO.236---------- 311122 ---------the NO.237---------- 311131 ---------the NO.238---------- 3112111 ---------the NO.239---------- 311212 ---------the NO.240---------- 311221 ---------the NO.241---------- 31123 ---------the NO.242---------- 311311 ---------the NO.243---------- 31132 ---------the NO.244---------- 3121111 ---------the NO.245---------- 312112 ---------the NO.246---------- 312121 ---------the NO.247---------- 31213 ---------the NO.248---------- 312211 ---------the NO.249---------- 31222 ---------the NO.250---------- 31231 ---------the NO.251---------- 313111 ---------the NO.252---------- 31312 ---------the NO.253---------- 31321 ---------the NO.254---------- 3133 ---------the NO.255---------- 3211111 ---------the NO.256---------- 321112 ---------the NO.257---------- 321121 ---------the NO.258---------- 32113 ---------the NO.259---------- 321211 ---------the NO.260---------- 32122 ---------the NO.261---------- 32131 ---------the NO.262---------- 322111 ---------the NO.263---------- 32212 ---------the NO.264---------- 32221 ---------the NO.265---------- 3223 ---------the NO.266---------- 32311 ---------the NO.267---------- 3232 ---------the NO.268---------- 331111 ---------the NO.269---------- 33112 ---------the NO.270---------- 33121 ---------the NO.271---------- 3313 ---------the NO.272---------- 33211 ---------the NO.273---------- 3322 ---------the NO.274---------- 3331

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值