當前位置:首頁 » 編程語言 » 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 瀏覽:753
沒有許可權訪問計算機 發布: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