Ejemplo n.º 1
0
    /**
     *  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");
        }
    }
Ejemplo n.º 2
0
 /**
  * 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;
 }
Ejemplo n.º 3
0
 /**
   * 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());
  }
Ejemplo n.º 4
0
 /**
  * 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;
 }
    /**/ 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());
    }
Ejemplo n.º 6
0
    /**/ public static void main(string[] strarr)
    {
        int    num  = 0;
        double num2 = (double)0f;
        double num3;

        while (!StdIn.isEmpty())
        {
            num3  = StdIn.readDouble();
            num2 += num3;
            num++;
        }
        num3 = num2 / (double)num;
        StdOut.println(new StringBuilder().append("Average is ").append(num3).toString());
    }
Ejemplo n.º 7
0
    /**
     * Reads in a sequence of real numbers from standard input and prints
     * out their average to standard output.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) { 
        int count = 0;       // number input values
        double sum = 0.0;    // sum of input values

        // read data and compute statistics
        while (!StdIn.isEmpty()) {
            double value = StdIn.readDouble();
            sum += value;
            count++;
        }

        // compute the average
        double average = sum / count;

        // print results
        StdOut.println("Average is " + average);
    }
Ejemplo n.º 8
0
    /**
     * 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);
    }
Ejemplo n.º 9
0
    /**/ 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);
    }
Ejemplo n.º 10
0
    /**/ 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))
        });
    }
Ejemplo n.º 11
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));
    }