227
来源:互联网 发布:淘宝背景音乐喵喵喵 编辑:程序博客网 时间:2024/06/05 03:28
5.10
我不会告诉你,其实我没有明白这道题是干嘛的。
这个题我瞅了很多天了,可是我的脑子里根本塞不进东西去。
最后参考别人的,反正是AC了,以后脑子清楚的时候,一定会回来再看这题的。
public class Tower { private Stack<Integer> disks; // create three towers (i from 0 to 2) public Tower(int i) { disks = new Stack<Integer>(); } // Add a disk into this tower public void add(int d) { if (!disks.isEmpty() && disks.peek() <= d) { System.out.println("Error placing disk " + d); } else { disks.push(d); } } // @param t a tower // Move the top disk of this tower to the top of t. public void moveTopTo(Tower t) { // Write your code here t.add(disks.pop()); } // @param n an integer // @param destination a tower // @param buffer a tower // Move n Disks from this tower to destination by buffer tower public void moveDisks(int n, Tower destination, Tower buffer) { // Write your code here if(n == 1){ moveTopTo(destination); } if(n >1){ moveDisks(n-1,buffer,destination); moveTopTo(destination); buffer.moveDisks(n-1, destination,this); } } public Stack<Integer> getDisks() { return disks; }}/** * Your Tower object will be instantiated and called as such: * Tower[] towers = new Tower[3]; * for (int i = 0; i < 3; i++) towers[i] = new Tower(i); * for (int i = n - 1; i >= 0; i--) towers[0].add(i); * towers[0].moveDisks(n, towers[2], towers[1]); * print towers[0], towers[1], towers[2]*/
0 0
- 227
- 227
- 227
- 227f1e27
- 227f1e27
- spoj 227
- UVA 227
- uva 227
- 227 - Puzzle
- 227 - Puzzle
- UVa 227
- SGU 227
- ocp-227
- UVA-227
- UVA 227
- leetOJ 227
- 227 - Puzzle
- UVA-227
- Android源码中编译自己的so库
- [转] java的System.getProperty()方法可以获取的值
- Servlet 客户端 HTTP 请求
- 561. Array Partition I
- 读书笔记(VI) 少有人走的路
- 227
- esyuitextbox失去焦点验证并提示
- MySQL性能指标
- javaweb中Cookie的使用
- Reverse Words in a String III
- golang-new与make
- 关于datetimepicker时间格式的问题
- 锁相环
- JSTL与EL表达式