public double robinRound(int[] arrival, int[] execute, int q) { if (arrival == null || arrival.length == 0 || execute == null || execute.length == 0) { return 0.0; } int currentTime = 0; int waitingTime = 0; int nextProIndex = 0; Queue<Process> queue = new LinkedList<Process>(); while (!queue.isEmpty() || nextProIndex < arrival.length) { if (!queue.isEmpty()) { Process cur = queue.poll(); waitingTime += currentTime - cur.arri; currentTime += Math.min(cur.exe, q); for (int i = nextProIndex; i < arrival.length; i++) { if (arrival[i] <= currentTime) { queue.offer(new Process(arrival[i], execute[i])); nextProIndex++; } else { break; } } if (cur.exe > q) { queue.offer(new Process(currentTime, cur.exe - q)); } } else { queue.offer(new Process(arrival[nextProIndex], execute[nextProIndex])); currentTime += arrival[nextProIndex++]; } } return (double) waitingTime / arrival.length; }
private void searchFor(int n, Searcher searcher) throws Exception { System.out.println("Searching for " + n); Hits hits = searcher.search(QueryParser.parse(intToEnglish(n), "contents", ANALYZER)); System.out.println("Search for " + n + ": total=" + hits.length()); for (int j = 0; j < Math.min(3, hits.length()); j++) { System.out.println("Hit for " + n + ": " + hits.doc(j).get("id")); } }
public int findMinPath(TreeNode root) { if (root == null) { return 0; } else if (root.left == null) { return findMinPath(root.right) + root.val; } else if (root.right == null) { return findMinPath(root.left) + root.val; } else { return Math.min(findMinPath(root.left), findMinPath(root.right)) + root.val; } }
private void loop() { speed = Math.min(speed + DELAY / 1000d / RAMP, targetPower); if (Math.abs(angleError(targetAngle)) > Math.PI) System.out.println(angleError(targetAngle)); if (Math.abs(angleError(targetAngle)) > Math.PI / 2) { steer.set( Math.max( -STEER_CAP, Math.min(STEER_CAP, STEER_GAIN * angleError(targetAngle - Math.PI)))); drive.set(-speed); } else { steer.set(Math.max(-STEER_CAP, Math.min(STEER_CAP, STEER_GAIN * angleError(targetAngle)))); drive.set(speed); } if (driveEncoder != null) { double enc = driveEncoder.get(); double angle = getAngle() - gyro.getAngle() * Math.PI / 180 + Math.PI / 2; x += (enc - encLast) * Math.cos(angle); y += (enc - encLast) * Math.sin(angle); encLast = enc; } try { Thread.sleep(DELAY); } catch (Exception e) { } }
private int findGCDInTwoNumber(int a, int b) { if (a == 0 || b == 0) { return 0; } if (a == 1 || b == 1) { return 1; } int max = Math.max(a, b); int min = Math.min(a, b); if (max % min != 0) { return findGCDInTwoNumber(min, max % min); } else { return min; } }
private void reportLatencies() { LOG.debug("Reporting latency for each message:"); int validEntries = 0; long totalTime = 0, min = Long.MAX_VALUE, max = 0; for (int i = 0; i < TEST_ITERATIONS; i++) { long latencyNanos = endTimeNanos[i] - startTimeNanos[i]; long time = TimeUnit.NANOSECONDS.toMicros(latencyNanos); if (time > 0) { totalTime += time; validEntries++; min = Math.min(min, time); max = Math.max(max, time); } LOG.debug("message #" + i + ": latency= " + time + " micros."); } String report = "%s iterations - latency in micros to send %s bytes via TCP/IP: avg=%s, min=%s, max=%s"; LOG.info( String.format(report, TEST_ITERATIONS, payload.length, totalTime / validEntries, min, max)); }