大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
package 约瑟夫环;
创新互联是专业的红旗网站建设公司,红旗接单;提供网站设计、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行红旗网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
import java.util.LinkedList;
import java.util.List;
/**
* 约瑟夫环问题的一种描述是:编号为1.2.3…….n的n个人按顺时针方向围坐一圈 ,每人手持一个密码(正整数),
* 开始任意选一个整数作为报数上限值,从第一个人开始顺时针自1开始顺序报数,报到m时停止报数。报m的人出列,
* 将他的密码作为新的m值,从他顺时针下一个人开始重新从1开始报数,
* 如此下去直到所有的人全部都出列为止。试设计程序实现,按照出列的顺序打印各人的编号。
* @author Administrator
*
*/
public class Question2 {
class person {
int password;
int number;
int state = 1;
public person(int password, int number) {
this.password = password;
this.number = number;
}
public person(int number){
this.number = number;
}
}
public int ListLength(Listperson list) {
int count = 0;
if (list != null) {
for (person p : list) {
if (p.state != 0) {
count++;
}
}
}
return count;
}
public void cacle() {
// 初始化数据
Listperson list = new LinkedListperson();
list.add(new person(3,1));
list.add(new person(1,2));
list.add(new person(7,3));
list.add(new person(2,4));
list.add(new person(4,5));
list.add(new person(8,6));
list.add(new person(4,7));
int position = -1;//初始位置
int m = 20; //第一次报多少的人出来
int count = 0;//已经报了多少人
while (ListLength(list) != 0) {
position = (position + 1) % list.size();// 位置定位
if (((person) list.get(position)).state != 0) {
count++;
}
if (count == m) {
person p = list.get(position);
System.out.print(p.number+" ");
p.state = 0;
m = p.password;
list.set(position, p);
count = 0;
}
}
}
public static void main(String[] args) {
Question2 q= new Question2();
q.cacle();
}
}
跟这差不多的。
无界面!
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class LinkCircle {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n, k, m, i, j, count, p, q;
int[] a = new int[100];
ListInteger array = new LinkedListInteger();
p = 0;
q = 0;
count = 0;
n = in.nextInt();
m = in.nextInt();
for (i = 0; i n; i++) {
array.add(i + 1);
System.out.printf(i != n - 1 ? "%d " : "%d\n", array.get(i));
}
int pos = -1;
int x = 0;
while(array.size() != 1)
{
//x = array.get(pos);
pos += m ;
if(pos = array.size())
{
pos = pos % array.size();
}
System.out.println("remove pos:" + (pos + 1) + ", value:" + array.get(pos));
array.remove(pos--);
System.out.print("Array : ");
for (i = 0; i array.size(); i++) {
System.out.printf(i != array.size() - 1 ? "%d " : "%d\n\n", array.get(i));
}
}
System.out.println("res:" + array.get(0));
}
}
看了你的代码,不是很明白,给你提几个建议吧:
1、不需要tail节点
2、remove方法应该对删除节点前面的节点操作,而不是使用数字找
给你我修改的LinkList类,你参考一下:
public class LinkList {
private Node head;
int curlen = 0;
// 创建链表
public void createlist(int code) throws Exception {
insert(curlen, code);
}
public void insert(int i, int code) throws Exception {
Node s = new Node(code);
if (i == 0) {
s.setNext(head);
head = s;
}
Node p = head;
int j = 0;
while (p != null j i - 1) {
p = p.getNext();
j++;
}
if (j i || p == null) {
throw new Exception("插入位置不合理");
}
s.setNext(p.getNext());
p.setNext(s);
// tail = s;
// tail.setNext(head);
curlen = curlen + 1;
}
public void remove(int i) throws Exception {
Node p = head, q = null;
int j = 0;
i = i - 1;
while (j i) {
q = p;
p = p.getNext();
j++;
}
if (j i || p == null)
throw new Exception("删除位置不合法");
if (q == null) {
// tail.setNext(p.getNext());
head = head.getNext();
} else
q.setNext(p.getNext());
curlen = curlen - 1;
}
/**
* 按照节点删除
* @param i
* @throws Exception
*/
public void remove(Node p) throws Exception {
if(p.getNext()==p){
p=null;
head=null;
}
else{
Node q = p.getNext();
p.setNext(q.getNext());
}
curlen = curlen - 1;
}
public void out(int m) throws Exception {
Node p = head;
if(m==1){
System.out.print("按照顺序出列");
return;
}
int count = 1;
int n=m-1;
while (curlen 0) {
if (count == n) {
System.out.print(p.getNext().getData() + " ");
remove(p);
count = 1;
} else {
count++;
}
p = p.getNext();
}
}
public void display() {
Node node = head;
for (int i = 0; i 2 * curlen; i++) {
System.out.print(node.getData() + " ");
node = node.getNext();
}
System.out.println();
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入总人数:");
int totalNum = scanner.nextInt();
System.out.print("请输入报数的大小:");
int cycleNum = scanner.nextInt();
System.out.print("请输入第几个报数:");
int cur = scanner.nextInt();
yuesefu(totalNum, cycleNum ,cur);
scanner.close();
}
public static void yuesefu(int totalNum, int countNum,int cur) {
// 初始化人数
ListInteger start = new ArrayListInteger();
for (int i = 1; i = totalNum; i++) {
start.add(i);
}
//从第K个开始计数
int k = cur-1;
while (start.size() 0) {
k = k + countNum;
//第m人的索引位置
k = k % (start.size()) - 1;
// 判断是否到队尾
if (k 0) {
System.out.println(start.get(start.size()-1));
start.remove(start.size() - 1);
k = 0;
} else {
System.out.println(start.get(k));
start.remove(k);
}
}
}
}