Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gasStation;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.StringTokenizer;
- public class GasStation {
- public static String calcularEstaciones(int kmDeposito, int distanciaViaje, String estaciones) {
- if (kmDeposito > distanciaViaje) {
- return "No stops";
- }
- int estacionAnterior = 0;
- int estacionActual = 0;
- int kmUltimaEstacion = 0;
- String solucion = "";
- StringTokenizer listaEstaciones = new StringTokenizer(estaciones);
- if (!listaEstaciones.hasMoreTokens()) {
- return "Error";
- }
- estacionActual = Integer.parseInt(listaEstaciones.nextToken());
- if (estacionActual > kmDeposito) {
- return "Error";
- }
- while (listaEstaciones.hasMoreTokens()) {
- estacionAnterior = estacionActual;
- estacionActual = Integer.parseInt(listaEstaciones.nextToken());
- if (estacionActual > kmUltimaEstacion + kmDeposito) {
- kmUltimaEstacion = estacionAnterior;
- solucion += estacionAnterior;
- if (kmUltimaEstacion + kmDeposito >= distanciaViaje) {
- return solucion;
- } else {
- solucion += " ";
- }
- }
- }
- return "Error final";
- }
- public static void main(String[] args) {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- try {
- int numeroCasos = Integer.parseInt(br.readLine().trim());
- for (int i=0; i<numeroCasos; i++) {
- int distanciaDeposito = Integer.parseInt(br.readLine().trim());
- int distanciaViaje = Integer.parseInt(br.readLine().trim());
- br.readLine();
- String estaciones = br.readLine();
- System.out.println(calcularEstaciones(distanciaDeposito, distanciaViaje, estaciones));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement