viernes, 19 de octubre de 2012

305 - Joseph, uva, solution

Just a simple simulation be aware that it would take so much time,
so as they are just 13 cases then, we calculate them so just will have
to output the memorized values.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Main {

 public static int solve(int k) {
  for (int m = 1; ; m++) {
   ArrayList<integer> lista = new ArrayList<integer>();
   for (int i = 1; i <= 2 * k; i++) {
    lista.add(i);
   }
   int idx = (m - 1) % lista.size();
   boolean exito = true;
   while (lista.size() > k && exito) {
    if (lista.get(idx) > k) {
     lista.remove(idx);
    }
    else exito = false;
    idx += m - 1;
    idx %= lista.size();
   }
   if (exito) return m;
  }
 }
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  int res[] = new int[14];
  for (int i = 1; i < 14; i++) {
   res[i] = solve(i);
  }
  int k = sc.nextInt();
  while (k != 0) {
   System.out.println(res[k]); 
   k = sc.nextInt();
  }
 }

}

No hay comentarios:

Publicar un comentario