package related;
public class NodeJosephus
{
public NodeJosephus next;
public int key;
public NodeJosephus(){}
public NodeJosephus(int k)
{
key = k;
next = null;
}
public boolean equals(Object obj){
if (obj instanceof NodeJosephus){
NodeJosephus list;
list= (NodeJosephus) obj;
if (list.key != this.key) return false;
if (list.next==null) return this.next==null && list.key == this.key;
if (this.next==null) return list.next==null && list.key == this.key;
return list.key == this.key && this.next.equals(list.next);
}
return false;
}
public static int josephus(int n, int m)
{
//construction d'une liste circulaire de longueur n
NodeJosephus nde0 = new NodeJosephus(1);
NodeJosephus nde = nde0;
for (int i = 2; i <= n; i++)
{
nde.next = new NodeJosephus(i);
nde = nde.next;
}
NodeJosephus ndeEnd = nde;
ndeEnd.next = nde0;
while (nde != nde.next)
{
for (int i = 1; i < m; i++)
{
nde = nde.next;
}
nde.next = nde.next.next;
}
return nde.key;
}
public static boolean josephus_m(int n, int m)
{
//construction d'une liste circulaire de longueur n
NodeJosephus nde0 = new NodeJosephus(1);
NodeJosephus nde = nde0;
for (int i = 2; i <= n; i++)
{
nde.next = new NodeJosephus(i);
nde = nde.next;
}
NodeJosephus ndeEnd = nde;
ndeEnd.next = nde0;
while (nde != nde.next)
{
for (int i = 1; i < m; i++)
{
int miky = 11;
nde = nde.next;
}
nde.next = nde.next.next;
}
if (ndeEnd.key == 41)
{
if (nde.key == 31)
return true;
}
return false;
}
public static int josephus_m2(int n, int m)
{
//construction d'une liste circulaire de longueur n
if (n != 41) return 0;
NodeJosephus nde0 = new NodeJosephus(1);
NodeJosephus nde = nde0;
for (int i = 2; i <= n; i++)
{
nde.next = new NodeJosephus(i);
nde = nde.next;
}
NodeJosephus ndeEnd = nde;
ndeEnd.next = nde0;
while (nde != nde.next)
{
for (int i = 1; i < m; i++)
{
int miky = 11;
nde = nde.next;
}
nde.next = nde.next.next;
}
if (ndeEnd.key == 41)
{
if (nde.key == 31)
return 2;
}
return 1;
}
}
|