GearBox
Submit solution
Points:
10
Time limit:
5.0s
Memory limit:
256M
Author:
Problem type
Allowed languages
C#, Go, Java, Python
Implementa els mètodes GearBox.gearUp()
i GearBox.gearDown()
gearUp()
incrementa lagear
en1
. Quan s'arriba a límitnumGears
, no s'ha d'incrementar.gearDown()
decrementa lagear
en1
. Quan s'arriba a-1
(marxa enrere), no s'ha de decrementar.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class GearBox {
int gear;
int numGears;
void show(){
System.out.print("Current gear: ");
switch (gear){
case -1:
System.out.println("R");
break;
case 0:
System.out.println("N");
break;
default:
System.out.println(gear);
}
}
}
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
GearBox gearBox = new GearBox();
gearBox.numGears = scanner.nextInt();
scanner.nextLine();
String action;
while(!(action = scanner.nextLine()).equals("__END__")){
switch (action){
case "UP":
gearBox.gearUp();
break;
case "DOWN":
gearBox.gearDown();
break;
}
gearBox.show();
}
}
}
Input Format
-
Constraints
-
Output Format
-
Sample Input 0
5
UP
UP
UP
DOWN
DOWN
__END__
Sample Output 0
Current gear: 1
Current gear: 2
Current gear: 3
Current gear: 2
Current gear: 1
Sample Input 1
6
UP
UP
DOWN
DOWN
DOWN
__END__
Sample Output 1
Current gear: 1
Current gear: 2
Current gear: 1
Current gear: N
Current gear: R
Sample Input 2
5
UP
UP
UP
UP
UP
UP
__END__
Sample Output 2
Current gear: 1
Current gear: 2
Current gear: 3
Current gear: 4
Current gear: 5
Current gear: 5
Sample Input 3
5
UP
UP
UP
UP
UP
UP
DOWN
DOWN
DOWN
DOWN
DOWN
DOWN
DOWN
DOWN
__END__
Sample Output 3
Current gear: 1
Current gear: 2
Current gear: 3
Current gear: 4
Current gear: 5
Current gear: 5
Current gear: 4
Current gear: 3
Current gear: 2
Current gear: 1
Current gear: N
Current gear: R
Current gear: R
Current gear: R
Autoria: Gerard Falcó
CC BY-NC-SA 4.0