当前位置:首页 » 编程语言 » java有n个人围成一圈

java有n个人围成一圈

发布时间: 2023-01-05 12:18:10

‘壹’ java编程 有n个人围成一个圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出

publicclassIncrease{

publicstaticvoidrep(boolean[]people){
inti=0,j=0,n=people.length,m=n;
while(n>2){
i=++i%m;
if(people[i]==true){
j++;
if(j==3){
people[i]=false;
System.out.println(i);
n--;//总人数减1
j=0;//到3从头数
}
}
}
}

publicstaticvoidmain(String[]args){
booleanpeople[]=newboolean[10];
for(inti=0;i<10;i++){
people[i]=true;
}
rep(people);
}
}

main函数为测试例子,打印结果如下

3

6

9

2

7

1

8

5

‘贰’ 用java解决约瑟夫问题

Java约瑟夫问题: n个人(不同id)围成一个圈,从startId(任意数)个开始报数m(任意数)个数,数m的人出列排成新队列,m清零,然后又从下一个人开始数m个数开始,数到m就出列接在新队列尾部,如此重复,知道所有人都出列为止。
package list;
import java.util.ArrayList;

* 打印 出列后的新队列
*
* eg
* int n = 10;//总人数
* int m = 3; //报数个数
* int startIndex = 1; //起点位置
* @author Hulk 2014 03 20
*
*/
public class JosephListTest {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
JosephListTest test = new JosephListTest();
int n = 10; //总人数
int m = 3; //报数个数
int startIndex = 12; //起点位置
System.out.println("JosephListTest: n= " + n + ", m= " + m +
", startIndex= " + startIndex + "\n\nQUEUE RESULT:");
ArrayList<Person> queueList = test.queuePreson(n, m, startIndex);
for (Person person : queueList) {
System.out.println("OUT person: " + person);
}
System.out.println("use time= " +
(System.currentTimeMillis() - startTime));
}
private ArrayList<Person> queuePreson(int n, int m, int startIndex) {
ArrayList<Person> queueList = null;
PersonList list = createList(n);
//list.search();
if ((list != null) && (list.head != null)) {
queueList = new ArrayList<JosephListTest.Person>();
PNode pNode = list.head;
if (startIndex > 0) {
startIndex = startIndex % n;
pNode = list.getNode(startIndex);
} else {
pNode = list.head;
}
int count = 1;
while (list.size > 0) {
Person outPerson = null;
//find
if (count == (m - 1)) {
//delete next node
Person prev = pNode.person;
outPerson = list.remove(prev);
queueList.add(outPerson);
//System.out.println("OUT person: " + outPerson + ", size= " + list.size);
count = 0;
}
pNode = pNode.next;
count++;
}
}
return queueList;
}
private PersonList createList(int n) {
PersonList list = new PersonList();
for (int i = 0; i < n; i++) {
Person person = new Person(i, "name_" + (i + 1));
list.add(i, person);
}
return list;
}
public class PersonList {
PNode head = null;
int size = 0;
public PersonList() {
}
public PersonList(Person person) {
head = new PNode(person, head);
size++;
}
public PersonList(PNode head) {
this.head = head;
head.setNext(head);
size++;
}
public PNode getHead() {
return head;
}
public void setHead(PNode head) {
this.head = head;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public void size(int size) {
this.size = size;
}
public boolean isEmpty() {
return this.size <= 0;
}
public void initHead(Person person) {
if (size == 0) {
head = new PNode(person, head);
} else {
PNode no = head;
head = new PNode(person, no);
}
size++;
}
public void add(int index, Person person) {
if (size == 0) {
head = new PNode(person, head);
head.setNext(head);
//System.out.println("head: " + head);
} else {
if (index < 0) {
index = 0;
}
if (index > size) {
index = size;
}
PNode no = head;
for (int i = 0; i < (index - 1); i++) {
no = no.next;
}
PNode newNode = new PNode(person, no.next);
no.next = newNode;
}
size++;
}
public Person delete(int index) {
PNode pNode = remove(index);
if ((pNode != null) && (pNode.next != null)) {
return pNode.next.person;
}
return null;
}
public PNode remove(int index) {
if (size == 0) {
return null;
} else {
if ((index < 0) || (index >= size)) {
return null;
}
}
PNode no = head;
for (int i = 0; i < (index - 1); i++) {
no = no.next;
}
no.next = no.next.next;
size--;
if ((no != null) && (no.next != null)) {
return no.next;
}
return null;
}
/**
* remove next node of person node, and return the deleted person
* @param prePerson
* @return removed Person
*/
public Person remove(Person prePerson) {
if (prePerson == null) {
return null;
}
if (size == 0) {
return null;
}
PNode preNode = head;
int index = -1;
for (int i = 0; i < size; i++) {
if (preNode.person.id == prePerson.id) {
index = i;
break;
} else {
preNode = preNode.next;
}
}
Person remPerson = null;
if (size <= 1) {
//only one node, get its person and set it as null
remPerson = preNode.person;
preNode = null;
} else {
//preNode.next.person is dest one
remPerson = preNode.next.person;
preNode.next = preNode.next.next;
}
size--;
//System.out.println("deleteing index= " + index + " : " + remPerson + ", size= " + size);
return remPerson;
}
public int update(Person src, Person dest) {
if (src == null) {
return -1;
}
int index = -1;
PNode no = head;
for (int i = 0; i < size; i++) {
if (src.id == no.person.id) {
no.person = dest;
break;
} else {
no = no.next;
}
}
return index;
}
public boolean set(int index, Person person) {
if (person == null) {
return false;
}
if (size == 0) {
return false;
} else {
if ((index < 0) || (index >= size)) {
return false;
}
}
PNode no = head;
for (int i = 0; i < index; i++) {
no = no.next;
}
no.person = person;
return true;
}
public Person get(int index) {
PNode no = getNode(index);
if (no != null) {
return no.person;
}
return null;
}
public PNode getNode(int index) {
if (size == 0) {
return null;
} else {
if ((index < 0) || (index >= size)) {
return null;
}
}
PNode no = head;
for (int i = 0; i < index; i++) {
no = no.next;
}
return no;
}
public void search() {
int sizeLong = size;
PNode no = head;
for (int i = 0; i < sizeLong; i++) {
System.out.println("search index= " + i + ", " + no);
no = no.next;
}
}
}
public class PNode {
Person person;
PNode next = null;
public PNode() {
}
public PNode(Person person) {
this.person = person;
}
public PNode(Person person, PNode next) {
this.person = person;
this.next = next;
}
@Override
public String toString() {
return "PNode [person=" + person.id + ", next=" + next.person.id +
"]";
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public PNode getNext() {
return next;
}
public void setNext(PNode next) {
this.next = next;
}
}
public class Person {
int id = 0;
String name = "";
public Person() {
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
}
}

‘叁’ 有N个人围成一圈每次从1数起数到3就把那个人踢出去最后只保留一个人,用java集合和scanner

按照你的要求编写的Java程序如下

importjava.util.ArrayList;
importjava.util.List;
importjava.util.Scanner;
publicclassDD{
publicstaticvoidmain(String[]args){
System.out.print("请输入人数");
Scannersc=newScanner(System.in);
intN=sc.nextInt();
List<Integer>l=newArrayList<Integer>();
for(inti=1;i<=N;i++){
l.add(i);
}
intpoint=0,number=1;
while(l.size()>1){
if(number%3==0){
l.remove(point);
--point;
}
++point;
++number;
if(point>l.size()-1){
point=0;
}
}
System.out.println("最后剩下的人为"+l.get(0)+"号");
}
}

运行结果

请输入人数100
最后剩下的人为91号

‘肆’ 用JAVA编写,有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈

大致的思路:定义list集合,利用for循环添加1—n元素,利用if判断,对3取余为0的删除,查看最终输出结果。试着自己做,不懂再问

‘伍’ java编程,有n个人围成一圈,顺序排号,从一号到n号,从第一个开始报数,(从1报到3)凡报到3的

这么经典的面向对象题目 以下代码仅供参考

importjava.util.Scanner;

publicclassMain{

publicstaticvoidmain(String[]args){
intn;
Scannerscanner=newScanner(System.in);
System.out.println("请输入一个正整数:");
n=scanner.nextInt();
scanner.close();

PersonQuanpersonQuan=newPersonQuan();
Personperson;
for(inti=1;i<=n;i++){
person=newPerson(i);
personQuan.addPerson(person);
}

n=0;
person=personQuan.first;
while(personQuan.first!=personQuan.last){
n++;
if(n%3==0){
//System.out.println("第"+n/3+"次移除编号:"+person.id);
personQuan.removePerson(person);
}
person=person.right;
}

System.out.println("最后留下的是第"+personQuan.first.id+"号");

}

}

classPerson{
intid;
Personleft;
Personright;

publicPerson(intid){
this.id=id;
}

}

classPersonQuan{

Personfirst;
Personlast;

publicvoidaddPerson(Personperson){
if(first==null){
first=person;
last=person;
person.left=person;
person.right=person;
}else{
last.right=person;
person.left=last;
person.right=first;
first.left=person;
last=person;
}
}

publicintremovePerson(Personperson){
if(first==last){
return0;
}

if(person==first){
last.right=person.right;
person.right.left=last;
first=person.right;
}elseif(person==last){
first.left=person.left;
person.left.right=first;
last=person.left;
}else{
person.left.right=person.right;
person.right.left=person.left;
}
return1;
}

}

‘陆’ n个小孩围成一个圈,从一开始数,数到七的人出列,求依次出列的次序 求用java写

这个问题是很经典的编程问题,叫约瑟夫环问题,我之前有写程序,所以直接拷给你吧,我初始的n值为13,你也可以自己修改 //YueSeFu.java public class YueSeFu { public static void main(String[] args) { final int n=13,s=1,m=5;//n为总人数,从第1个人开始报数,报数到m的出圈 int[] p=new int;//数组p用于标记已出圈的人 int[] q=new int;//数组q存放出队的顺序 int i,j,k,N=0; k=s-2;//k从1开始数出圈人的下标 for(i=1;i

热点内容
随机启动脚本 发布:2025-07-05 16:10:30 浏览:525
微博数据库设计 发布:2025-07-05 15:30:55 浏览:24
linux485 发布:2025-07-05 14:38:28 浏览:304
php用的软件 发布:2025-07-05 14:06:22 浏览:754
没有权限访问计算机 发布:2025-07-05 13:29:11 浏览:430
javaweb开发教程视频教程 发布:2025-07-05 13:24:41 浏览:695
康师傅控流脚本破解 发布:2025-07-05 13:17:27 浏览:239
java的开发流程 发布:2025-07-05 12:45:11 浏览:684
怎么看内存卡配置 发布:2025-07-05 12:29:19 浏览:282
访问学者英文个人简历 发布:2025-07-05 12:29:17 浏览:833