Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.anlak.optimization;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
- /*
- * There are p knights sitting at positions [1,2,...p] at King Arthur's round
- * table. In order to choose the leader, Arthur starts counting at position 1
- * and dismisses every rth seated knight until only 1 knight remains. The
- * remaining seated knight becomes the leader. Let C(p,r) be the integer
- * position of the eventual leader when the table starts out with p knights and
- * every rth seated one is dismissed. Let T =
- * C(999,11)+C(121,12)+C(2,1)+C(15,16)+C(99,3). What is T? Details and
- * assumptions C(4,2)=1. The knights start seated at positions 1, 2, 3, 4.
- * Arthur dismisses them in this order: 2, 4, 3. The knight seated at position 1
- * becomes the leader.
- */
- public class Brillant
- {
- public static int c( int n, int r )
- {
- List<Integer> x = new LinkedList<Integer>();
- for (int i = 1; i <= n; i++)
- x.add(i);
- Iterator<Integer> iter = x.listIterator(n);
- while (x.size() > 1)
- {
- for (int i = 0; i < r; i++)
- {
- if (!iter.hasNext())
- iter = x.iterator();
- iter.next();
- }
- iter.remove();
- }
- return x.get(0);
- }
- public static void main( String[] args )
- {
- System.out.println(c(999, 11) + c(121, 12) + c(2, 1) + c(15, 16) + c(99, 3));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement