2021-10-13
作者:互联网
HDU1443-java循环双链表实现
题目大意:有2k个人,前k个是好人,后k个是坏人,每隔m个人去除一个人,求k个坏人在好人前全部被去除的最小m。
代码如下:
import java.util.ArrayList;
import java.util.Scanner;
public class HDU1433{
public static void main(String []arges) {
Scanner input = new Scanner(System.in);
ArrayList ans=new ArrayList();
while (!input.hasNext(“0”)) {
int n = input.nextInt();
int m = 1;
joseph j = new joseph(n, m);
man k = j.first;
for (int i = 1; i <= j.jlength(); i++) {
k = k.next;
}
ans.add(j.circle(n,m));
}
for(int i=0;i<ans.size();i++){
System.out.println(ans.get(i));
}
}
}
class man{
int no;
man next;
man prior;
public man(int num){
no=num;
next=null;
prior=null;
}
}
class joseph{
int n,m;
man first;
public joseph(int n1,int m1){
man p,t;
n=n1*2;
m=m1;
first=new man(1);
t=first;
for(int i=1;i<n;i++){
p=new man(i+1);
t.next=p;
p.prior=t;
t=p;
}
t.next=first;
first.prior=t;
}
public int jlength(){
int length=1;
man q=first;
while(q.next!=first){
length++;
q=q.next;
}
return length;
}
public int circle(int n,int m) {
joseph q=this;
man c = q.first;
while (q.jlength()!=n) {
for (int i = 1; i < m; i++) {
c = c.next;
}
while (c.no > n && q.jlength()>n) {
c.prior.next = c.next;
c.next.prior = c.prior;
c=c.next;
for (int i = 1; i < m; i++) {
c = c.next;
}
}
if ((c.no <= n )&& (q.jlength()>n)) {
m=m+1;
q=new joseph(n,m);
c=q.first;
}
}
return m;
}
}
标签:10,13,int,next,prior,joseph,2021,new,man 来源: https://blog.csdn.net/m0_62049544/article/details/120743759