Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package reddit;
- import java.math.BigDecimal;
- import java.util.Scanner;
- public class RedditChallengeIntermediate118 {
- public static void main(String[] args) {
- RedditChallengeIntermediate118 rc118 = new RedditChallengeIntermediate118();
- Scanner scan = new Scanner(System.in);
- String input = scan.nextLine();
- //String input = "5.00 2.00 2.00 1.00";
- //rc118.calculate(input.split(" "));
- //input = "99.99 1.23 3.21 5.01";
- rc118.calculate(input.split(" "));
- }
- public void calculate(String[] inputs) {
- int simLength = new BigDecimal(inputs[0]).multiply(new BigDecimal(100)).setScale(0).intValueExact();
- int shellRetrieval = new BigDecimal(inputs[1]).multiply(new BigDecimal(100)).setScale(0).intValueExact();
- int propellantRetrieval = new BigDecimal(inputs[2]).multiply(new BigDecimal(100)).setScale(0).intValueExact();
- int firingTime = new BigDecimal(inputs[3]).multiply(new BigDecimal(100)).setScale(0).intValueExact();
- int shotsFired = 0;
- boolean cannonLoaded = false;
- boolean cannonFired = false;
- boolean cannonReady = true;
- int cannonTimer = 0;
- boolean shellLoaded = false;
- int shellTimer = 0;
- boolean propellantLoaded = false;
- int propellantTimer = 0;
- //System.out.println("Length of simulation: " + simLength);
- //System.out.println("Length of shell retrieval: " + shellRetrieval);
- //System.out.println("Length of propellant retrieval: " + propellantRetrieval);
- //System.out.println("Length of firingTime: " + firingTime);
- for (int timer = 1; timer <= simLength; timer++) {
- if (cannonFired) cannonTimer++;
- if (!shellLoaded) shellTimer++;
- if (!propellantLoaded) propellantTimer++;
- if (shellTimer == shellRetrieval) shellLoaded = true;
- if (propellantTimer == propellantRetrieval) propellantLoaded = true;
- if (!cannonReady) {
- if (cannonTimer == firingTime) {
- cannonReady = true;
- cannonTimer = 0;
- cannonFired = false;
- }
- }
- if (cannonReady) {
- if (shellLoaded && propellantLoaded) cannonLoaded = true;
- if (cannonLoaded) {
- shotsFired++;
- cannonFired = true;
- cannonLoaded = false;
- cannonReady = false;
- shellLoaded = false;
- shellTimer = 0;
- propellantLoaded = false;
- propellantTimer = 0;
- }
- }
- }
- System.out.println("Number of shots fired: " + shotsFired);
- }
- }
Add Comment
Please, Sign In to add comment