
正文
hanoi代码java hanlp java
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
求JAVA汉诺塔相关的socket代码
定义hanoi代码java了服务器线程类hanoi代码java,服务器运行在一个单独的线程中。
客户端运行在主线程中。
所有代码放在一个源文件中就行。源文件名是Hanoi.java
下面是源代码hanoi代码java,输入的盘子数不要太大,20以内,否则会步数太多,输出耗时太久。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.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();
}
}
}
}
相关问答
Q1: java 汉诺塔问题
ABC做3个stack栈
在A中初始化 3 2 1,
A to C 就是 c.push(a.pop());
A to B 就是 b.push(a.pop())
B to C 就是 c.push(b.pop())
就这样就能得到结果了。不需要去管移动的是那个数
Q2: java中汉诺塔的算法问题
class HanRuoTa {
static long s=0;
public static void main(String args[]) {
int n =3;
System.out.println("汉诺塔层数为" + n);
System.out.println("移动方案为:" );
hanoi(n, 'a', 'b', 'c');
System.out.println("需要移动次数:"+s);
}
static void hanoi(int n, char a, char b, char c) {
if (n 0) {
hanoi(n - 1, a, c, b);
move(a, b);
hanoi(n - 1, c, b, a);
s++;
}
}
static void move(char x, char y) {
System.out.println(x + "-" + y + "\t");
}
}
运行结果:
汉诺塔层数为3
移动方案为:
a-b
a-c
b-c
a-b
c-a
c-b
a-b
需要移动次数:7
Q3: 汉罗塔hanoi
楼主写的程序是正确的,不知道你是什么意思/
public class Hanoi {
public static void main(String args[]){
int n=3;
hanoi(n,'A','B','C');
}
static void moves(char a,char c){
System.out.println("From"+a+"To"+c);
}
static void hanoi(int n,char a,char b,char c){
if(n==1){
moves(a,c);
}
else{
hanoi(n-1,a,c,b);
moves(a,c);
hanoi(n-1,b,a,c);
}
}
}
这个是Java的
Q4: 用java编写hanoi塔的非递归算法。
这是个好问题hanoi代码java,很少看到有人写汉诺塔hanoi代码java的非递归...其实只要先写出递归hanoi代码java,然后把递归hanoi代码java的每一步要做的事情记录在一个栈里面就可以hanoi代码java了
public class Test {
private static void emitStep(int source, int dest) {
System.out.println(source + " - " + dest);
}
static class Step {
Step(int n, int s, int d, int t) {
this.n = n;
source = s;
dest = d;
temp = t;
}
int n, source, dest, temp;
}
private static void hanoi(int n, int source, int dest, int temp) {
java.util.StackStep steps = new java.util.StackStep();
steps.add(new Step(n, source, dest, temp));
while (steps.empty() == false) {
Step step = steps.pop();
if (step.n == 1) {
emitStep(step.source, step.dest);
continue;
}
steps.push(new Step(step.n - 1, step.temp, step.dest, step.source));
steps.push(new Step(1, step.source, step.dest, 0));
steps.push(new Step(step.n - 1, step.source, step.temp, step.dest));
}
}
public static void main(String[] args) {
hanoi(3, 1, 3, 2);
}
}
Q5: 怎样用Java编写汉诺塔程序
package Hanoi;
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("请输入盘数hanoi代码java:");
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);
}
}
}
hanoi代码java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于hanlp java、hanoi代码java的信息别忘了在本站进行查找喔。








