java实现约瑟夫环问题(采用链式存储结构

1.大二狗技术菜得很,写的代码又臭又烂,大神们莫要嘲笑

2.应付数据结构课来着

3.虽然今天我很垃圾,但我相信明天的我会更垃圾(滑稽

4.之所以用java是因为我想写个GUI,好看点

package joseph;

public class Main1 {

	public static void main(String[] args) {
		Jiemian jiemian=new Jiemian();
		// TODO Auto-generated method stub

	}

}

然后是逻辑结构上的重点:


package joseph;

public class LinkedList {
	Node Head=null;
	StringBuffer sBuffer ;//存放输出次序字符串
public class  Node{
	Node next;
	int data;
	public Node() {
		next=null;
		data=999;
	}
	public Node(int data) {
		this.data=data;
		next=null;
	}
}
public LinkedList() {
	
}
public void intial(int n) {//n表示约瑟夫环的元素数,即总人数
	if(n<=0) {
		System.out.println("输入人数小于等于0");
		
	}
	else {
		for(int i=1;i<=n;i++) {
			Node newnode=new Node(i);
			if(Head==null) {
				Head=new Node();
				//System.out.println("aaa");
			}
			Node temp=Head;
			while(temp.next!=null) {
				temp=temp.next;
				
			}
			temp.next=newnode;
		}
		Node temp1=Head;
		while(temp1.next!=null) {
			temp1=temp1.next;
			//System.out.println("aaa");
		}
		temp1.next=Head;
		//System.out.println("aaa");
	}
}//创建单向循环链表方法结束
public void test() {//测试用方法
	Node temp=Head;
	

		for(int i=1;i<=8;i++) {
		System.out.println(temp.next.data);
		temp=temp.next;
		}
	}
Node Find(int key) {
	if(key!=0) {
		Node p=Head.next;
		while((p!=Head)&&(p.data!=key)) {
			p=p.next;
		}
		if(p.data==key) {
			//System.out.println("aaa");
			return p;
		}else {System.out.println("key的值大于表长,没有找到");
			return null;}
	}
	else if(key==0) {
		//System.out.println("bbb");
		return Head;
		
	}
	else {
		System.out.println("出错了,不应该出现这句话");
		return null;
	}
	
}
Node inform(Node p,int m) {
	int tag=1;
	for(;tag<m;) {
		p=p.next;
		if(p==Head) {
			p=p.next;
		}
		tag++;
	}
	return p;
}
void Joseph(int n,int m,int key) {
 sBuffer = new StringBuffer(" ");
	int x,y;
	int z=0;
	Node p,q;
	intial(n);
	 p=Find(key);
	 q=Find(key-1);
	for(int flag=0;flag<n;flag++) {
		Node pp=inform(p, m);
		Node qq=inform(q, m);
	 x=qq.data;
	 y=pp.data;
	 qq.next=pp.next;
	 System.out.print(y+" ");
	 sBuffer.append(y+" ");
		if(qq.next==Head) {qq=qq.next;}
		z=qq.next.data;
		p=Find(z);
		q=Find(x);
	}
		
}
	

}

然后是界面,写的傻不拉几的

package joseph;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;





public class Jiemian  extends JFrame{
JPanel jp1,jp2;
JPanel jp11,jp12,jp13,jp14;
JLabel n_label;
JLabel m_label;
JLabel key_label;
JTextField n_textfiled;
JTextField m_textfiled;
JTextField key_textfiled;
JLabel result_label;
JTextField result_textfiled;
JButton queding_button;
public Jiemian() {
	jp1=new JPanel();
	jp2=new JPanel();
	jp11=new JPanel();
	jp12=new JPanel();
	jp13=new JPanel();
	jp14=new JPanel();
	n_label=new JLabel("要参加约瑟夫环的人数n");
	n_textfiled=new JTextField(8);
	m_label=new JLabel("要数的数m");
	m_textfiled=new JTextField(8);
	key_label=new JLabel("开始游戏的人的编号key");
	key_textfiled=new JTextField(8);
	queding_button=new JButton("输出次序");
	result_label=new JLabel("输出次序结果");
	result_textfiled=new JTextField(50);
	
	jp1.setLayout(new GridLayout(1, 4));
	jp11.setLayout(new FlowLayout(FlowLayout.CENTER, 5,5));
	jp12.setLayout(new FlowLayout(FlowLayout.CENTER, 5,5));
	jp12.setLayout(new FlowLayout(FlowLayout.CENTER, 5,5));
	jp14.setLayout(new FlowLayout(FlowLayout.CENTER, 5,5));
	jp2.setLayout(new FlowLayout(FlowLayout.CENTER, 15,15));
	jp1.add(jp11);
	jp1.add(jp12);
	jp1.add(jp13);
	jp1.add(jp14);
	jp11.add(n_label);
	jp11.add(n_textfiled);
	jp12.add(m_label);
	jp12.add(m_textfiled);
	jp13.add(key_label);
	jp13.add(key_textfiled);
	jp14.add(queding_button);
	jp2.add(result_label);
	jp2.add(result_textfiled);
	//System.out.println("1111");
	result_textfiled.setEditable(false);
	
	Container con=this.getContentPane();
	con.setLayout(new BorderLayout());
	con.add(jp1,BorderLayout.NORTH);
	con.add(jp2,BorderLayout.SOUTH);
	n_label.setFont(new Font(Font.MONOSPACED, Font.BOLD, 15));
	m_label.setFont(new Font(Font.MONOSPACED, Font.BOLD, 15));
	key_label.setFont(new Font(Font.MONOSPACED, Font.BOLD, 15));
	result_label.setFont(new Font(Font.MONOSPACED, Font.BOLD, 15));
	queding_button.setFont(new Font(Font.MONOSPACED, Font.BOLD, 15));

	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setVisible(true);
	this.setBounds(300,200,1500,200);
	// TODO Auto-generated constructor stub
	queding_button.addActionListener(new shuchu());


}class shuchu implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		int m=0,n=0,key=0;
		try {
	m=Integer.valueOf(m_textfiled.getText().toString()).intValue();
	n=Integer.valueOf(n_textfiled.getText().toString()).intValue();
	key=Integer.valueOf(key_textfiled.getText().toString()).intValue();}
		catch(NumberFormatException ee){
		result_textfiled.setText("您输入的参数中含有非数字字符");
		}
		if(n<=0) {
			System.out.println("输入人数n应该大于等于1,而您输入了小于等于0的数");
			result_textfiled.setText("输入人数n应该大于等于1,而您输入了小于等于0的数");
			
		}else if(m<=0) {
			System.out.println("要计数的数字m必须大于等于1");

			result_textfiled.setText("要计数的数字m必须大于等于1");
		}
		else if((key<=0)||(key>n)) {
			System.out.println("错误!要选择的开始游戏的人的编号必须在1~n之间");
			result_textfiled.setText("错误!要选择的开始游戏的人的编号必须在1~n之间");
		}else {
			LinkedList linkedlist=new LinkedList();
			linkedlist.Joseph(n, m, key);
			result_textfiled.setText(linkedlist.sBuffer.toString());
		}
		
		
	}
	
}


}

猜你喜欢

转载自blog.csdn.net/hrwy2920566283/article/details/80152910