Advertisement
javipinero

Gas Stations Java Tuenti Contest

Jun 21st, 2011
1,462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. package gasStation;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.util.StringTokenizer;
  6.  
  7. public class GasStation {
  8.    
  9.     public static String calcularEstaciones(int kmDeposito, int distanciaViaje, String estaciones) {
  10.        
  11.         if (kmDeposito > distanciaViaje) {
  12.             return "No stops";
  13.         }
  14.        
  15.         int estacionAnterior = 0;
  16.         int estacionActual = 0;
  17.         int kmUltimaEstacion = 0;
  18.         String solucion = "";
  19.        
  20.         StringTokenizer listaEstaciones = new StringTokenizer(estaciones);
  21.         if (!listaEstaciones.hasMoreTokens()) {
  22.             return "Error";
  23.         }
  24.        
  25.         estacionActual = Integer.parseInt(listaEstaciones.nextToken());
  26.        
  27.         if (estacionActual > kmDeposito) {
  28.             return "Error";
  29.         }
  30.        
  31.         while (listaEstaciones.hasMoreTokens()) {
  32.             estacionAnterior = estacionActual;
  33.             estacionActual = Integer.parseInt(listaEstaciones.nextToken());
  34.             if (estacionActual > kmUltimaEstacion + kmDeposito) {
  35.                 kmUltimaEstacion = estacionAnterior;
  36.                 solucion += estacionAnterior;
  37.                 if (kmUltimaEstacion + kmDeposito >= distanciaViaje) {
  38.                     return solucion;
  39.                 } else {
  40.                     solucion += " ";
  41.                 }
  42.             }
  43.         }
  44.        
  45.         return "Error final";
  46.     }
  47.    
  48.     public static void main(String[] args) {
  49.        
  50.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  51.        
  52.         try {
  53.            
  54.             int numeroCasos = Integer.parseInt(br.readLine().trim());
  55.            
  56.             for (int i=0; i<numeroCasos; i++) {
  57.                
  58.                 int distanciaDeposito = Integer.parseInt(br.readLine().trim());
  59.                 int distanciaViaje = Integer.parseInt(br.readLine().trim());
  60.                 br.readLine();
  61.                 String estaciones = br.readLine();
  62.                 System.out.println(calcularEstaciones(distanciaDeposito, distanciaViaje, estaciones));
  63.             }
  64.            
  65.         } catch (Exception e) {
  66.             e.printStackTrace();
  67.         }
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement