3.4

Topic: In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (e.g., each disk sits on top of an even larger one). You have the following constraints:

1)Only one disk can be moved at a time.

2)A disk is slid off the top of one rod onto the next rod.

3)A disk can only be placed on top of a larger disk.

Write a program to move the disks from the first rod to the last using Stacks

// 方法1递归

moveDisks(int n, Tower from, Tower to, Tower buffer){

1)move top n-1 disks from from to buffer;(Recursion)

2)move disk n from from to to;

3)move n-1 disks from buffer to to;

}

// 用LinkedList来表示Tower
import java.util.LinkedList;
public class Tower {
private LinkedList<Integer> tower;
	
	public Tower() {
		tower = new LinkedList<Integer>();
	}

	public boolean add(int disk) {//把每个子塔搭起来
		if (tower.isEmpty() || tower.peek() > disk) {
			tower.push(disk);
			return true;
		}
		return false;
	}

	public boolean moveTo(Tower to) {//只把最上面的移一次
		if (tower.isEmpty()) {
			return false;
		}
		return to.add(tower.pop());
	}

	// toString是Object类的方法,LinkedList继承,returns a string representation of this collection. 
	//这个方法在这里并不需要,LinkedList直接就能打印
	public String toString() {
		return tower.toString();
	}
	}
public class TowersOfHanoi {
	public static void moveDisks(int n, Tower from, Tower to, Tower buffer) {
        // n==1,是上一个子循环结束的条件;n==0,是这个方法结束的条件。
		if (n == 0) {//这是不正常情况,n是不能为0的,根本不用移,跟n不能小于0一样。
			return;
		}
		if (n == 1) {// 这是结束情况,最后剩一块了,移它就行
			from.moveTo(to);
			return;
		}

		moveDisks(n - 1, from, buffer, to);
		from.moveTo(to);
		moveDisks(n - 1, buffer, to,from);
	}
	
	public static void main(String[] args){
		Tower from=new Tower();
		Tower to=new Tower();
		Tower buffer=new Tower();
		for(int i=4;i>=0;i--){
		from.add(i);
		}
		System.out.println(from.toString());
		System.out.println(buffer.toString());
		System.out.println(to.toString());
		moveDisks(5,from,to,buffer);
		System.out.println(from.toString());
		System.out.println(buffer.toString());
		System.out.println(to.toString());
	}
}
//结果
[0, 1, 2, 3, 4]
[]
[]
[]
[]
[0, 1, 2, 3, 4]




As Mirth Corporation (now is a subsidiary of Quality Systems, Inc.) says on their web-site, “Mirth Connect is the Swiss Army knife of healthcare integration engines, specifically designed for HL7 message integration. It provides the necessary tools for developing, testing, deploying, and monitoring interfaces. And because it’s open source, you get all of the advantages of a large community of users with commercial quality support.” In addition, “The 2014 HL7 Interface Technology Survey Results” show that Mirth Connect is one of the fastest growing healthcare messaging platforms due to its open source paradigm, and robust functionality for HL7 messaging and X12 documents. Mirth Connect also speeds up the development of interfaces for data exchange across different formats and diverse healthcare systems environment. This book describes version 3.x of Mirth Connect to the point that reader are confident enough to start building their own healthcare data exchange interfaces and transforming various versions of HL7 messages. As you read this book, you will be implementing a fictitious Eligibility Query Service. Each connection point (channel) is explained in a separate chapter, which in turn provides step-by-step instructions on how to create and code data transformation rules. This book is written using Mirth Connect 3.4.0.8000 version of the product. Consequently, other releases may include new features, or features used in this book may change or disappear. You may also notice some differences between screen shots provided in the book and those you see when using Mirth Connect.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值