Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Gauntlet {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int numAvengers = sc.nextInt(); // this stores the value of N
- int numRounds = sc.nextInt(); // this store the value of K
- sc.nextLine(); // blank line to flush the output stream
- String cmds = sc.nextLine(); // read list of commands, store in a String
- String[] cmdList = cmds.split(" "); // Splitting the commands and storing each command in an array.
- int i;
- //Declares Stack
- Stack<Integer> commandStack = new Stack<Integer>();
- int currentAvenger = 0;
- long start = System.currentTimeMillis(); // start measuring runtime.
- //for (i = 0; i < numRounds; i++) {
- //Loop through cmdList
- for (int j = 0; j <= cmdList.length - 1; j++) {
- //Evaluate command to add or subtract from Stack
- if(cmdList[j].equals("undo")) {
- commandStack.remove(0);
- }
- else {
- //Converts string to integer and adds to the Stack
- commandStack.add(0, Integer.valueOf(cmdList[j]));
- }
- int sum = 0;
- //Sum each element in Stack
- for(i = 0; i < commandStack.size(); i++){
- sum = sum + commandStack.get(i);
- }
- currentAvenger = sum % numAvengers;
- //System.out.println("Current Avenger " + currentAvenger);
- }
- // your code goes here.
- // This loop should iterate over the cmdList
- // and determine who gets the gauntlet after each command
- // all operations inside this loop should take constant time.
- //}
- // //Declares sum
- // int sum = 0;
- // //Sum each element in Stack
- // for(i = 0; i < commandStack.size(); i++){
- // sum = sum + commandStack.get(i);
- // }
- //Gets specific end Avenger from Mod
- // int currentAvenger = sum % numAvengers;
- System.out.println("The Gauntlet is with Avenger " + currentAvenger);
- long end = System.currentTimeMillis(); // stop measuring time
- System.out.println("Running time " + (end - start) + "ms"); // this will print the running time of your loop.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement