Advertisement
javipinero

Christmas Lights Java Tuenti Contest

Jun 21st, 2011
1,485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. package christmaslight;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5.  
  6. public class ChristmasLight {
  7.  
  8.     public static boolean esPar(int value) {
  9.         if (value%2 == 0) {
  10.             return true;
  11.         }
  12.         return false;
  13.     }
  14.    
  15.     public static int menor(int a, int b) {
  16.         if (a<b) {
  17.             return a;
  18.         }
  19.         return b;
  20.     }
  21.    
  22.     public static String calcularEncendidas(int numBombillas, int numInstante) {
  23.         String solucion = "";
  24.        
  25.         if (numInstante == 0) {
  26.             return "All lights are off :(";
  27.         }
  28.         int i = 0;
  29.        
  30.         if (esPar(numInstante)) {
  31.             i++;
  32.         }
  33.        
  34.         int hasta = menor(numInstante-1, numBombillas-2);
  35.        
  36.         for (;i<hasta;i+=2) {
  37.             solucion += i + " ";
  38.         }
  39.        
  40.         if (i == hasta) {
  41.             solucion += i;
  42.         }
  43.        
  44.         if (solucion.isEmpty()) {
  45.             solucion = "All lights are off :(";
  46.         }
  47.        
  48.         return solucion;
  49.     }
  50.    
  51.     public static void main(String[] args) {
  52.        
  53.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  54.        
  55.         try {
  56.             int numCasos = Integer.parseInt(br.readLine());
  57.             for (int i=0; i<numCasos; i++) {
  58.                 int numBombillas = Integer.parseInt(br.readLine());
  59.                 int numInstante = Integer.parseInt(br.readLine());
  60.                 System.out.println(calcularEncendidas(numBombillas, numInstante));
  61.             }
  62.            
  63.         } catch (Exception e) {
  64.             e.printStackTrace();
  65.         }
  66.  
  67.     }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement