/** * Reads the currency exchange table from standard input and * prints an arbitrage opportunity to standard output (if one exists). * * @param args the command-line arguments */ public static void main(String[] args) { // V currencies int V = StdIn.readInt(); String[] name = new String[V]; // create complete network EdgeWeightedDigraph G = new EdgeWeightedDigraph(V); for (int v = 0; v < V; v++) { name[v] = StdIn.readString(); for (int w = 0; w < V; w++) { double rate = StdIn.readDouble(); DirectedEdge e = new DirectedEdge(v, w, -Math.log(rate)); G.addEdge(e); } } // find negative cycle BellmanFordSP spt = new BellmanFordSP(G, 0); if (spt.hasNegativeCycle()) { double stake = 1000.0; for (DirectedEdge e : spt.negativeCycle()) { StdOut.printf("%10.5f %s ", stake, name[e.from()]); stake *= Math.exp(-e.weight()); StdOut.printf("= %10.5f %s\n", stake, name[e.to()]); } } else { StdOut.println("No arbitrage opportunity"); } }
/** * Reads a 1D array of booleans from standard input and returns it. * * @return the 1D array of booleans */ public static boolean[] readBoolean1D() { int n = StdIn.readInt(); boolean[] a = new boolean[n]; for (int i = 0; i < n; i++) { a[i] = StdIn.readBoolean(); } return a; }
/** * Reads a 1D array of integers from standard input and returns it. * * @return the 1D array of integers */ public static int[] readInt1D() { int n = StdIn.readInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = StdIn.readInt(); } return a; }
/** * Reads a 1D array of doubles from standard input and returns it. * * @return the 1D array of doubles */ public static double[] readDouble1D() { int n = StdIn.readInt(); double[] a = new double[n]; for (int i = 0; i < n; i++) { a[i] = StdIn.readDouble(); } return a; }
/** * Unit tests the {@code FarthestPair} data type. * Reads in an integer {@code n} and {@code n} points (specified by * their <em>x</em>- and <em>y</em>-coordinates) from standard input; * computes a farthest pair of points; and prints the pair to standard * output. * * @param args the command-line arguments */ public static void main(String[] args) { int n = StdIn.readInt(); Point2D[] points = new Point2D[n]; for (int i = 0; i < n; i++) { int x = StdIn.readInt(); int y = StdIn.readInt(); points[i] = new Point2D(x, y); } FarthestPair farthest = new FarthestPair(points); StdOut.println(farthest.distance() + " from " + farthest.either() + " to " + farthest.other()); }
/** * Reads a 2D array of booleans from standard input and returns it. * * @return the 2D array of booleans */ public static boolean[][] readBoolean2D() { int m = StdIn.readInt(); int n = StdIn.readInt(); boolean[][] a = new boolean[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { a[i][j] = StdIn.readBoolean(); } } return a; }
/** * Reads a 2D array of integers from standard input and returns it. * * @return the 2D array of integers */ public static int[][] readInt2D() { int m = StdIn.readInt(); int n = StdIn.readInt(); int[][] a = new int[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { a[i][j] = StdIn.readInt(); } } return a; }
/** * Reads a 2D array of doubles from standard input and returns it. * * @return the 2D array of doubles */ public static double[][] readDouble2D() { int m = StdIn.readInt(); int n = StdIn.readInt(); double[][] a = new double[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { a[i][j] = StdIn.readDouble(); } } return a; }
/** * Unit tests the {@code ClosestPair} data type. * Reads in an integer {@code n} and {@code n} points (specified by * their <em>x</em>- and <em>y</em>-coordinates) from standard input; * computes a closest pair of points; and prints the pair to standard * output. * * @param args the command-line arguments */ public static void main(String[] args) { int n = StdIn.readInt(); Point2D[] points = new Point2D[n]; for (int i = 0; i < n; i++) { double x = StdIn.readDouble(); double y = StdIn.readDouble(); points[i] = new Point2D(x, y); } ClosestPair closest = new ClosestPair(points); StdOut.println(closest.distance() + " from " + closest.either() + " to " + closest.other()); }
/** * Reads in a sequence of integers from the whitelist file, specified as * a command-line argument. Reads in integers from standard input and * prints to standard output those integers that are not in the file. * * @param args the command-line arguments */ public static void main(String[] args) { In in = new In(args[0]); int[] white = in.readAllInts(); StaticSETofInts set = new StaticSETofInts(white); // Read key, print if not in whitelist. while (!StdIn.isEmpty()) { int key = StdIn.readInt(); if (!set.contains(key)) StdOut.println(key); } }
/** * Unit tests the {@code GrahamScan} data type. * Reads in an integer {@code n} and {@code n} points (specified by * their <em>x</em>- and <em>y</em>-coordinates) from standard input; * computes their convex hull; and prints out the points on the * convex hull to standard output. * * @param args the command-line arguments */ public static void main(String[] args) { int n = StdIn.readInt(); Point2D[] points = new Point2D[n]; for (int i = 0; i < n; i++) { int x = StdIn.readInt(); int y = StdIn.readInt(); points[i] = new Point2D(x, y); } GrahamScan graham = new GrahamScan(points); for (Point2D p : graham.hull()) StdOut.println(p); }
/** * Reads in a sequence of pairs of integers (between 0 and n-1) from standard input, * where each integer represents some object; * if the sites are in different components, merge the two components * and print the pair to standard output. * * @param args the command-line arguments */ public static void main(String[] args) { int n = StdIn.readInt(); WeightedQuickUnionUF uf = new WeightedQuickUnionUF(n); while (!StdIn.isEmpty()) { int p = StdIn.readInt(); int q = StdIn.readInt(); if (uf.connected(p, q)) continue; uf.union(p, q); StdOut.println(p + " " + q); } StdOut.println(uf.count() + " components"); }
/**/ public static void main(string[] strarr) { int num = StdIn.readInt(); Point2D[] array = new Point2D[num]; for (int i = 0; i < num; i++) { int num2 = StdIn.readInt(); int num3 = StdIn.readInt(); array[i] = new Point2D((double)num2, (double)num3); } FarthestPair farthestPair = new FarthestPair(array); StdOut.println(new StringBuilder().append(farthestPair.distance()).append(" from ").append(farthestPair.either()).append(" to ").append(farthestPair.other()).toString()); }
/**/ public static void main(string[] strarr) { int num = StdIn.readInt(); Point2D[] array = new Point2D[num]; for (int i = 0; i < num; i++) { double d = StdIn.readDouble(); double d2 = StdIn.readDouble(); array[i] = new Point2D(d, d2); } ClosestPair closestPair = new ClosestPair(array); StdOut.println(new StringBuilder().append(closestPair.distance()).append(" from ").append(closestPair.either()).append(" to ").append(closestPair.other()).toString()); }
/** * Reads in a sequence of integers from the whitelist file, specified as * a command-line argument; reads in integers from standard input; * prints to standard output those integers that do <em>not</em> appear in the file. * * @param args the command-line arguments */ public static void main(String[] args) { // read the integers from a file In in = new In(args[0]); int[] whitelist = in.readAllInts(); // sort the array Arrays.sort(whitelist); // read integer key from standard input; print if not in whitelist while (!StdIn.isEmpty()) { int key = StdIn.readInt(); if (BinarySearch.indexOf(whitelist, key) == -1) StdOut.println(key); } }
/** * Unit tests the {@code CollisionSystem} data type. * Reads in the particle collision system from a standard input * (or generates {@code N} random particles if a command-line integer * is specified); simulates the system. * * @param args the command-line arguments */ public static void main(String[] args) { StdDraw.setCanvasSize(600, 600); // enable double buffering StdDraw.enableDoubleBuffering(); // the array of particles Particle[] particles; // create n random particles if (args.length == 1) { int n = Integer.parseInt(args[0]); particles = new Particle[n]; for (int i = 0; i < n; i++) particles[i] = new Particle(); } // or read from standard input else { int n = StdIn.readInt(); particles = new Particle[n]; for (int i = 0; i < n; i++) { double rx = StdIn.readDouble(); double ry = StdIn.readDouble(); double vx = StdIn.readDouble(); double vy = StdIn.readDouble(); double radius = StdIn.readDouble(); double mass = StdIn.readDouble(); int r = StdIn.readInt(); int g = StdIn.readInt(); int b = StdIn.readInt(); Color color = new Color(r, g, b); particles[i] = new Particle(rx, ry, vx, vy, radius, mass, color); } } // create collision system and simulate CollisionSystem system = new CollisionSystem(particles); system.simulate(10000); }
/**/ public static void main(string[] strarr) { int num = StdIn.readInt(); int num2 = 2 * num; int num3 = 2 * num + 1; EdgeWeightedDigraph edgeWeightedDigraph = new EdgeWeightedDigraph(2 * num + 2); for (int i = 0; i < num; i++) { double d = StdIn.readDouble(); edgeWeightedDigraph.addEdge(new DirectedEdge(num2, i, (double)0f)); edgeWeightedDigraph.addEdge(new DirectedEdge(i + num, num3, (double)0f)); edgeWeightedDigraph.addEdge(new DirectedEdge(i, i + num, d)); int num4 = StdIn.readInt(); for (int j = 0; j < num4; j++) { int i2 = StdIn.readInt(); edgeWeightedDigraph.addEdge(new DirectedEdge(num + i, i2, (double)0f)); } } AcyclicLP acyclicLP = new AcyclicLP(edgeWeightedDigraph, num2); StdOut.println(" job start finish"); StdOut.println("--------------------"); for (int k = 0; k < num; k++) { StdOut.printf("%4d %7.1f %7.1f\n", new object[] { Integer.valueOf(k), java.lang.Double.valueOf(acyclicLP.distTo(k)), java.lang.Double.valueOf(acyclicLP.distTo(k + num)) }); } StdOut.printf("Finish time: %7.1f\n", new object[] { java.lang.Double.valueOf(acyclicLP.distTo(num3)) }); }
/**/ public static void main(string[] strarr) { StdDraw.setXscale(0.045454545454545456, 0.95454545454545459); StdDraw.setYscale(0.045454545454545456, 0.95454545454545459); StdDraw.show(0); Particle[] array; if (strarr.Length == 1) { int num = Integer.parseInt(strarr[0]); array = new Particle[num]; for (int i = 0; i < num; i++) { array[i] = new Particle(); } } else { int num = StdIn.readInt(); array = new Particle[num]; for (int i = 0; i < num; i++) { double d = StdIn.readDouble(); double d2 = StdIn.readDouble(); double d3 = StdIn.readDouble(); double d4 = StdIn.readDouble(); double d5 = StdIn.readDouble(); double d6 = StdIn.readDouble(); int r = StdIn.readInt(); int g = StdIn.readInt(); int b = StdIn.readInt(); Color c = new Color(r, g, b); array[i] = new Particle(d, d2, d3, d4, d5, d6, c); } } CollisionSystem collisionSystem = new CollisionSystem(array); collisionSystem.simulate(10000.0); }
/** * Reads the precedence constraints from standard input * and prints a feasible schedule to standard output. * * @param args the command-line arguments */ public static void main(String[] args) { // number of jobs int n = StdIn.readInt(); // source and sink int source = 2*n; int sink = 2*n + 1; // build network EdgeWeightedDigraph G = new EdgeWeightedDigraph(2*n + 2); for (int i = 0; i < n; i++) { double duration = StdIn.readDouble(); G.addEdge(new DirectedEdge(source, i, 0.0)); G.addEdge(new DirectedEdge(i+n, sink, 0.0)); G.addEdge(new DirectedEdge(i, i+n, duration)); // precedence constraints int m = StdIn.readInt(); for (int j = 0; j < m; j++) { int precedent = StdIn.readInt(); G.addEdge(new DirectedEdge(n+i, precedent, 0.0)); } } // compute longest path AcyclicLP lp = new AcyclicLP(G, source); // print results StdOut.println(" job start finish"); StdOut.println("--------------------"); for (int i = 0; i < n; i++) { StdOut.printf("%4d %7.1f %7.1f\n", i, lp.distTo(i), lp.distTo(i+n)); } StdOut.printf("Finish time: %7.1f\n", lp.distTo(sink)); }