DV 알고리즘 을 모 의 하 는 프로그램
import java.net.*;
import java.util.*;
public class Node0{
int[] m = new int[4];
int[] i = new int[4];
int fasong = 1;
int jieshou = 0;
int gaibian = 0;
int n = 0;
int flag = 0;
DatagramSocket serverSocket = new DatagramSocket(9000);;
String send;
byte[] sendData;
byte[] receiveData;
DatagramPacket sendPacket;
DatagramPacket receivePacket;
StringTokenizer st;
public void init0() throws Exception{
m[0] = 0;
m[1] = 1;
m[2] = 3;
m[3] = 7;
send=m[0]+" "+m[1]+" "+m[2]+" "+m[3]+" ";
System.out.println(" :");
System.out.println(" node1 node2 node3");
System.out.println("0 1 3 7");
System.out.println("---- ---- ---- ----");
System.out.println();
sendData = new byte[100];
receiveData = new byte[100];
sendData = send.getBytes();
InetAddress sendto = InetAddress.getByName("localhost");
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9001);
serverSocket.send(sendPacket);
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9002);
serverSocket.send(sendPacket);
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9003);
serverSocket.send(sendPacket);
}
public void update0() throws Exception{
send=m[0]+" "+m[1]+" "+m[2]+" "+m[3];
sendData = new byte[100];
sendData = send.getBytes();
InetAddress sendto = InetAddress.getByName("localhost");
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9001);
serverSocket.send(sendPacket);
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9002);
serverSocket.send(sendPacket);
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9003);
serverSocket.send(sendPacket);
fasong++;
}
public static void main(String args[]) throws Exception
{
new Node0();
}
public Node0() throws Exception{
try
{
Thread.currentThread();
Thread.sleep(20000);//
}
catch(Exception e){}
init0();
while(true){
flag = 0;
receivePacket = new DatagramPacket(receiveData,receiveData.length );
serverSocket.receive(receivePacket);
jieshou++;
String sentence=new String(receivePacket.getData());
st = new StringTokenizer(sentence);
n = 0;
while(st.hasMoreTokens() && n<4){
i[n] = Integer.parseInt(st.nextToken());
n++;
}
if(i[0] == 1){
if(m[1]+i[2]<m[2]){
m[2] = m[1] + i[2];
flag = 1;
System.out.println(" 1-》2 ");
}
if(m[1]+i[3]<m[3]){
m[3] = m[1] + i[3];
flag = 1;
System.out.println(" 1-》3 ");
}
}
if(i[0] == 2){
if(m[2]+i[2]<m[1]){
m[1] = m[2] + i[2];
flag = 1;
System.out.println(" 2-》1 ");
}
if(m[2]+i[3]<m[3]){
m[3] = m[2] + i[3];
flag = 1;
System.out.println(" 2-》3 ");
}
}
if(i[0] == 3){
if(m[3]+i[2]<m[1]){
m[1] = m[3] + i[2];
flag = 1;
System.out.println(" 3-》1 ");
}
if(m[3]+i[3]<m[2]){
m[2] = m[3] + i[3];
flag = 1;
System.out.println(" 3-》2 ");
}
}
if(flag == 1){
update0();
System.out.println(m[0]+" "+m[1]+" "+m[2]+" "+m[3]);
System.out.println("---- ---- ---- ----");
System.out.println(" ");
}
System.out.println();System.out.println();
}
}
}
import java.net.*;
import java.util.*;
public class Node1{
int[] m = new int[4];
int[] i = new int[4];
int n;
int flag;
DatagramSocket serverSocket;
String send;
byte[] sendData;
byte[] receiveData;
DatagramPacket sendPacket;
DatagramPacket receivePacket;
StringTokenizer st;
public void init1() throws Exception{
m[0] = 1;
m[1] = 1;
m[2] = 1;
m[3] = 999;
send=m[0]+" "+m[1]+" "+m[2]+" "+m[3]+" ";
System.out.println(" :");
System.out.println(" node0 node2 node3");
System.out.println("1 1 1 999");
System.out.println("---- ---- ---- ----");
System.out.println();
sendData = new byte[100];
receiveData = new byte[100];
sendData = send.getBytes();
InetAddress sendto = InetAddress.getByName("localhost");
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9000);
serverSocket.send(sendPacket);
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9002);
serverSocket.send(sendPacket);
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9003);
serverSocket.send(sendPacket);
}
public void update1() throws Exception{
send=m[0]+" "+m[1]+" "+m[2]+" "+m[3];
sendData = new byte[100];
sendData = send.getBytes();
InetAddress sendto = InetAddress.getByName("localhost");
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9000);
serverSocket.send(sendPacket);
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9002);
serverSocket.send(sendPacket);
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9003);
serverSocket.send(sendPacket);
}
public static void main(String args[]) throws Exception
{
new Node1();
}
public Node1() throws Exception{
serverSocket = new DatagramSocket(9001);
try
{
Thread.currentThread();
Thread.sleep(20000);//
}
catch(Exception e){}
init1();
while(true){
flag = 0;
receivePacket = new DatagramPacket(receiveData,receiveData.length );
serverSocket.receive(receivePacket);
String sentence=new String(receivePacket.getData());
st = new StringTokenizer(sentence);
n = 0;
while(st.hasMoreTokens() && n<4){
i[n] = Integer.parseInt(st.nextToken());
n++;
}
if(i[0] == 0){
if(m[1]+i[2]<m[2]){
m[2] = m[1] + i[2];
flag = 1;
System.out.println(" 0-》2 ");
}
if(m[1]+i[3]<m[3]){
m[3] = m[1] + i[3];
flag = 1;
System.out.println(" 0-》3 ");
}
}
if(i[0] == 2){
if(m[2]+i[1]<m[1]){
m[1] = m[2] + i[1];
flag = 1;
System.out.println(" 2-》1 ");
}
if(m[2]+i[3]<m[3]){
m[3] = m[2] + i[3];
flag = 1;
System.out.println(" 2-》3 ");
}
}
if(flag == 1){
update1();
System.out.println(m[0]+" "+m[1]+" "+m[2]+" "+m[3]);
System.out.println("---- ---- ---- ----");
System.out.println(" ");System.out.println();
}
}
}
}
import java.net.*;
import java.util.*;
public class Node2{
int[] m = new int[4];
int[] i = new int[4];
int n;
int flag;
DatagramSocket serverSocket;
String send;
byte[] sendData;
byte[] receiveData;
DatagramPacket sendPacket;
DatagramPacket receivePacket;
StringTokenizer st;
public void init2() throws Exception{
m[0] = 2;
m[1] = 3;
m[2] = 1;
m[3] = 2;
send=m[0]+" "+m[1]+" "+m[2]+" "+m[3]+" ";
System.out.println(" :");
System.out.println(" node0 node1 node3");
System.out.println("2 3 1 2");
System.out.println("---- ---- ---- ----");
System.out.println();
sendData = new byte[100];
receiveData = new byte[100];
sendData = send.getBytes();
InetAddress sendto = InetAddress.getByName("localhost");
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9000);
serverSocket.send(sendPacket);
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9001);
serverSocket.send(sendPacket);
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9003);
serverSocket.send(sendPacket);
}
public void update2() throws Exception{
send=m[0]+" "+m[1]+" "+m[2]+" "+m[3];
sendData = new byte[100];
sendData = send.getBytes();
InetAddress sendto = InetAddress.getByName("localhost");
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9000);
serverSocket.send(sendPacket);
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9001);
serverSocket.send(sendPacket);
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9003);
serverSocket.send(sendPacket);
}
public static void main(String args[]) throws Exception
{
Node2 node = new Node2();
}
public Node2() throws Exception{
serverSocket = new DatagramSocket(9002);
try
{
Thread.currentThread();
Thread.sleep(20000);//
}
catch(Exception e){}
init2();
while(true){
flag = 0;
receivePacket = new DatagramPacket(receiveData,receiveData.length );
serverSocket.receive(receivePacket);
String sentence=new String(receivePacket.getData());
st = new StringTokenizer(sentence);
n = 0;
while(st.hasMoreTokens() && n<4){
i[n] = Integer.parseInt(st.nextToken());
n++;
}
if(i[0] == 0){
if(m[1]+i[1]<m[2]){
m[2] = m[1] + i[1];
flag = 1;
System.out.println(" 0-》1 ");
}
if(m[1]+i[3]<m[3]){
m[3] = m[1] + i[3];
flag = 1;
System.out.println(" 0-》3 ");
}
}
if(i[0] == 1){
if(m[2]+i[1]<m[1]){
m[1] = m[2] + i[1];
flag = 1;
System.out.println(" 1-》0 ");
}
if(m[2]+i[3]<m[3]){
m[3] = m[2] + i[3];
flag = 1;
System.out.println(" 1-》3 ");
}
}
if(i[0] == 3){
if(m[3]+i[1]<m[1]){
m[1] = m[3] + i[1];
flag = 1;
System.out.println(" 3-》0 ");
}
if(m[3]+i[2]<m[2]){
m[2] = m[3] + i[2];
flag = 1;
System.out.println(" 3-》2 ");
}
}
if(flag == 1){
update2();
System.out.println(m[0]+" "+m[1]+" "+m[2]+" "+m[3]);
System.out.println("---- ---- ---- ----");
System.out.println(" ");System.out.println();
}
}
}
}
package com.hzy;
import java.net.*;
import java.util.*;
public class Node3{
int[] m = new int[4];
int[] i = new int[4];
int n;
int flag;
DatagramSocket serverSocket;
String send;
byte[] sendData;
byte[] receiveData;
DatagramPacket sendPacket;
DatagramPacket receivePacket;
StringTokenizer st;
public void init3() throws Exception{
m[0] = 3;
m[1] = 7;
m[2] = 999;
m[3] = 2;
send=m[0]+" "+m[1]+" "+m[2]+" "+m[3]+" ";
System.out.println(" :");
System.out.println(" node0 node1 node2");
System.out.println("3 7 999 2");
System.out.println("---- ---- ---- ----");
System.out.println();
sendData = new byte[100];
receiveData = new byte[100];
sendData = send.getBytes();
InetAddress sendto = InetAddress.getByName("localhost");
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9000);
serverSocket.send(sendPacket);
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9002);
serverSocket.send(sendPacket);
}
public void update3() throws Exception{
send=m[0]+" "+m[1]+" "+m[2]+" "+m[3];
sendData = new byte[100];
sendData = send.getBytes();
InetAddress sendto = InetAddress.getByName("localhost");
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9000);
serverSocket.send(sendPacket);
sendPacket = new DatagramPacket(sendData,sendData.length,sendto,9002);
serverSocket.send(sendPacket);
}
public static void main(String args[]) throws Exception
{
Node3 node = new Node3();
}
public Node3() throws Exception{
serverSocket = new DatagramSocket(9003);
try
{
Thread.currentThread();
Thread.sleep(20000);//
}
catch(Exception e){}
init3();
while(true){
flag = 0;
receivePacket = new DatagramPacket(receiveData,receiveData.length );
serverSocket.receive(receivePacket);
String sentence=new String(receivePacket.getData());
st = new StringTokenizer(sentence);
n = 0;
while(st.hasMoreTokens() && n<4){
i[n] = Integer.parseInt(st.nextToken());
n++;
}
if(i[0] == 0){
if(m[1]+i[1]<m[2]){
m[2] = m[1] + i[1];
flag = 1;
System.out.println(" 0-》1 ");
}
if(m[1]+i[2]<m[3]){
m[3] = m[1] + i[2];
flag = 1;
System.out.println(" 0-》2 ");
}
}
if(i[0] == 2){
if(m[3]+i[1]<m[1]){
m[1] = m[3] + i[1];
flag = 1;
System.out.println(" 2-》0 ");
}
if(m[3]+i[2]<m[2]){
m[2] = m[3] + i[2];
flag = 1;
System.out.println(" 2-》1 ");
}
}
if(flag == 1){
update3();
System.out.println(m[0]+" "+m[1]+" "+m[2]+" "+m[3]);
System.out.println("---- ---- ---- ----");
System.out.println(" ");System.out.println();
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.