汉诺塔java语言代码 汉诺塔java语言代码大全
怎样用Java编写汉诺塔程序
package Hanoi;
创新互联是一家专业提供五大连池企业网站建设,专注与成都网站建设、做网站、H5高端网站建设、小程序制作等业务。10年已为五大连池众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class Hanoi {
public static void main(String args[]) throws IOException {
Hanoi aa = new Hanoi();
aa.go();
}
public void go() throws IOException {
int n;
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入盘数:");
n = Integer.parseInt(buf.readLine());
Hanoi hanoi = new Hanoi();
hanoi.move(n, 'A', 'B', 'C');
}
public void move(int n, char a, char b, char c) {
if (n == 1) {
System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);
} else {
move(n - 1, a, c, b);
System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);
move(n - 1, b, a, c);
}
}
}
求一个汉诺塔的JAVA代码
public class Hannoi {
public static void main(String[] args) {
int nDisks = 3;
moveDish(nDisks, 'A', 'B', 'C');
}
private static void moveDish(int level, char from, char inter, char to) {
if (level == 1) {
System.out.println("从" + from + "移动盘子 " + level + " 号到" + to);
} else {
moveDish(level - 1, from, to, inter);
System.out.println("从" + from + "移动盘子 " + level + " 号到" + to);
moveDish(level - 1, inter, from, to);
}
}
}
求JAVA汉诺塔相关的socket代码
定义了服务器线程类,服务器运行在一个单独的线程中。
客户端运行在主线程中。
所有代码放在一个源文件中就行。源文件名是Hanoi.java
下面是源代码,输入的盘子数不要太大,20以内,否则会步数太多,输出耗时太久。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java点虐 .InetAddress;
import java点虐 .ServerSocket;
import java点虐 .Socket;
import java点虐 .UnknownHostException;
import java.util.Scanner;
public class Hanoi {
public static void main(String[] args) {
//创建服务器
HanoiServer server = new HanoiServer();
server.start();//启动服务器
/*开始创建客户端*/
Socket socket = null;//客户端Socket
try {
socket = new Socket(InetAddress.getLocalHost(), 8888);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream printStream = new PrintStream(socket.getOutputStream());
Scanner scanner=new Scanner(System.in);
System.out.println("请输入盘子数(3-10),数字太大,运算时间就会太长可能会卡死。");
printStream.print(scanner.nextInt());
printStream.println();
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
System.out.println("客户端socket关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class HanoiServer extends Thread {//服务器线程类
private ServerSocket serverSocket;
public HanoiServer() {
try {
this.serverSocket = new ServerSocket(8888);
} catch (IOException e) {
e.printStackTrace();
}
}
private void hanoi(int n, String from, String inter, String to, PrintStream printStream) {
if (n == 1) {
printStream.print("Disk 1 from " + from + " to " + to);
printStream.println();
} else {
hanoi(n - 1, from, to, inter, printStream);
printStream.print("Disk " + n + " from " + from + " to " + to);
printStream.println();
hanoi(n - 1, inter, from, to, printStream);
}
}
@Override
public void run() {
Socket socket = null;
try {
socket = this.serverSocket.accept();
PrintStream printStream = new PrintStream(socket.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
int n = Integer.parseInt(reader.readLine());
this.hanoi(n, "A", "B", "C", printStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
System.out.println("服务器socket关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
try {
this.serverSocket.close();
System.out.println("服务器serverSocket关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
java实现汉诺塔的代码,求注释,具体到每一行代码,急求,,,
这样应该可以了
如果还有那个地方不懂的,建议你研究下汉诺塔算法
import
java.io.BufferedReader;//引入IO包中的BufferedReader
import
java.io.IOException;//引入IO包中的IO异常处理
import
java.io.InputStreamReader;//引入IO包中的InputStreaReader
public
class
Hinoi
{
//主类
static
int
m=0;//定义移动的次数
//主程序入口--main方法
public
static
void
main(String[]
args)
{
//创建BufferedReader对象,InputStream输入流
BufferedReader
bf
=
new
BufferedReader(new
InputStreamReader(System.in));
System.out.println("请输入盘子的个数:");
try
{
int
sl
=
Integer.parseInt(bf.readLine().toString());//接收总盘子个数
toMove(sl,"A","B","C");//调用移动方法
A--C
}
catch
(NumberFormatException
e)
{捕获NumberFormatException异常
//
TODO
Auto-generated
catch
block
e.printStackTrace();//打印异常
}
catch
(IOException
e)
{//捕获IOException异常
//
TODO
Auto-generated
catch
block
e.printStackTrace();//打印异常
}
System.out.println("总共移动了:"+m+"
次数");//打印移动次数
}
//移动方法
private
static
void
toMove(int
sl,
String
one,
String
two,String
three)
{
if(sl==1){//如果只有一个盘子,则直接移动到C柱
System.out.println("盘子"+sl+"
从
"+one+"----"+three);
}else{//如果总盘数大于1,则递归调用移动方法
//把所有的数量为sl-1的盘子全部从A移到到B(C作为一个过渡),好提供一个最下面的位置给最大盘子到C;
toMove(sl-1,one,three,two);
System.out.println("盘子"+sl+"
从
"+one+"----"+three);
//把所有的剩余的盘子从B移动到C(A作为一个过渡)
toMove(sl-1,two,one,three);
}
m++;
}
}
本文题目:汉诺塔java语言代码 汉诺塔java语言代码大全
本文地址:http://azwzsj.com/article/ddjshch.html