コード例 #1
1
ファイル: Flips.java プロジェクト: wusung/algorithm-example
 public static void main(String[] args) {
   int T = Integer.valueOf(args[0]);
   Counter heads = new Counter("heads");
   Counter tails = new Counter("tails");
   for (int i = 0; i < T; i++) {
     if (StdRandom.bernoulli(0.5)) {
       heads.increment();
     } else {
       tails.increment();
     }
   }
   StdOut.println(heads);
   StdOut.println(tails);
   int d = heads.tally() - tails.tally();
   StdOut.println("delta:" + java.lang.Math.abs(d));
 }
コード例 #2
0
  public static void main(String[] args) // test client (optional)
      {
    final int num_tests = 1000;
    final int grid_size = 20;

    StdOut.println("Begin Generating Random Sites...");

    int row, col, count;
    double sum = 0.0;

    for (int i = 0; i < num_tests; i++) {
      count = 0;
      Percolation p = new Percolation(grid_size);
      while (!p.percolates()) {
        // choose site at random
        row = StdRandom.uniform(1, grid_size + 1);
        col = StdRandom.uniform(1, grid_size + 1);

        if (!p.isOpen(row, col)) {
          p.open(row, col);
          count++;
        }
      }
      sum += count;
    }

    StdOut.println("Average number of sites opened: " + sum / num_tests);
  }
コード例 #3
0
  /** Unit tests the <tt>GabowSCC</tt> data type. */
  public static void main(String[] args) {
    In in = new In(args[0]);
    Digraph G = new Digraph(in);
    GabowSCC scc = new GabowSCC(G);

    // number of connected components
    int M = scc.count();
    StdOut.println(M + " components");

    // compute list of vertices in each strong component
    Queue<Integer>[] components = (Queue<Integer>[]) new Queue[M];
    for (int i = 0; i < M; i++) {
      components[i] = new Queue<Integer>();
    }
    for (int v = 0; v < G.V(); v++) {
      components[scc.id(v)].enqueue(v);
    }

    // print results
    for (int i = 0; i < M; i++) {
      for (int v : components[i]) {
        StdOut.print(v + " ");
      }
      StdOut.println();
    }
  }
コード例 #4
0
 /** Unit tests the <tt>NonrecursiveDirectedDFS</tt> data type. */
 public static void main(String[] args) {
   In in = new In(args[0]);
   Digraph G = new Digraph(in);
   int s = Integer.parseInt(args[1]);
   NonrecursiveDirectedDFS dfs = new NonrecursiveDirectedDFS(G, s);
   for (int v = 0; v < G.V(); v++) if (dfs.marked(v)) StdOut.print(v + " ");
   StdOut.println();
 }
コード例 #5
0
ファイル: IkisiniTopla.java プロジェクト: erdemoncel/java
 public static void main(String[] args) {
   In giris = new In(args[0]);
   int[] a = giris.readAllInts();
   Saat saat = new Saat();
   int sayac = say(a);
   StdOut.println("geçen süre = " + saat.gecenSure());
   StdOut.println(sayac);
 }
コード例 #6
0
ファイル: StdOut.java プロジェクト: sunz7/algorithmCourse
  /** Simple unit test for <tt>StdOut</tt>. */
  public static void main(String[] args) {

    // write to stdout
    StdOut.println("Test");
    StdOut.println(17);
    StdOut.println(true);
    StdOut.printf("%.6f\n", 1.0 / 7.0);
  }
コード例 #7
0
ファイル: Kuyruk.java プロジェクト: erdemoncel/java
 public static void main(String[] args) {
   Kuyruk<String> k = new Kuyruk<String>();
   while (!StdIn.isEmpty()) {
     String eleman = StdIn.readString();
     if (!eleman.equals("-")) k.ekle(eleman);
     else if (!k.bosMu()) StdOut.print(k.cikar() + " ");
   }
   StdOut.println("\n(Kuyrukta " + k.genislik() + " tane eleman kaldı)");
 }
コード例 #8
0
ファイル: Task_43.java プロジェクト: HatlessFox/SelfStudy
  public static void main(String[] args) {
    if (args.length < 1) {
      StdOut.println("Args: <dir>");
      System.exit(-1);
    }

    Queue<String> fnames = new Queue<>();
    listFiles(new File(args[0]), fnames, 0);
    StdOut.println(CliUtils.iterableToString(fnames));
  }
コード例 #9
0
ファイル: Meter.java プロジェクト: KoHHeKT/Algs4
 public static void main(String[] args) {
   int N = 10;
   StdOut.println("iteration; size; time");
   for (int i = 0; i < 17; i++) {
     double time = TrialQuickUnionUF(N);
     if (i == 0) continue;
     StdOut.println(String.format("%d; %d; %f", i, N, time));
     N += N;
   }
 }
コード例 #10
0
ファイル: Queue.java プロジェクト: hhyifeng/algorithm
 /** Unit tests the <tt>Queue</tt> data type. */
 public static void main(String[] args) {
   Queue<String> q = new Queue<String>();
   while (!StdIn.isEmpty()) {
     String item = StdIn.readString();
     if (!item.equals("-")) // "-"作为输入结束标志
     q.enqueue(item);
     else if (!q.isEmpty()) StdOut.print(q.dequeue() + " ");
   }
   StdOut.println("(" + q.size() + " left on queue)");
 }
コード例 #11
0
 public static int rank(int key, int[] a, int lo, int hi, int deep) {
   for (int i = 0; i < deep; i++) {
     StdOut.print(" ");
   }
   StdOut.printf("%d %d %d\n", lo, hi, deep);
   if (lo > hi) return -1;
   int mid = lo + (hi - lo) / 2;
   if (key < a[mid]) return rank(key, a, lo, mid - 1, ++deep);
   else if (key > a[mid]) return rank(key, a, mid + 1, hi, ++deep);
   else return mid;
 }
コード例 #12
0
 /** Unit tests the Point data type. */
 public static void main(String[] args) {
   /* YOUR CODE HERE */
   Point p1 = new Point(1, 1);
   Point p2 = new Point(2, 2);
   Point p3 = new Point(7, 3);
   // int cmp = p1.compareTo(p2);
   // StdOut.println(cmp);
   double slp1 = p1.slopeTo(p2);
   StdOut.println(slp1);
   StdOut.println(p1.slopeTo(p3));
 }
コード例 #13
0
ファイル: Task_43.java プロジェクト: HatlessFox/SelfStudy
 public static void cliMode() {
   while (true) {
     StdOut.print("=> Enter folder name: ");
     String folder_name = StdIn.readString();
     Queue<String> fnames = new Queue<>();
     listFiles(new File(folder_name), fnames, 0);
     StdOut.println("<= Files: ");
     StdOut.println(CliUtils.iterableToString(fnames));
     StdOut.println();
   }
 }
コード例 #14
0
 public static void main(String[] args) {
   In in = new In(args[0]);
   String[] dictionary = in.readAllStrings();
   BoggleSolver solver = new BoggleSolver(dictionary);
   BoggleBoard board = new BoggleBoard(args[1]);
   int score = 0;
   for (String word : solver.getAllValidWords(board)) {
     StdOut.println(word);
     score += solver.scoreOf(word);
   }
   StdOut.println("Score = " + score);
 }
コード例 #15
0
ファイル: LinkedBag.java プロジェクト: zyearn/algs4
  /** Unit tests the <tt>LinkedBag</tt> data type. */
  public static void main(String[] args) {
    LinkedBag<String> bag = new LinkedBag<String>();
    while (!StdIn.isEmpty()) {
      String item = StdIn.readString();
      bag.add(item);
    }

    StdOut.println("size of bag = " + bag.size());
    for (String s : bag) {
      StdOut.println(s);
    }
  }
コード例 #16
0
 /**
  * Reads in a sequence of pairs of integers (between 0 and N-1) from standard input, where each
  * integer represents some object; if the objects are in different components, merge the two
  * components and print the pair to standard output.
  */
 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");
 }
コード例 #17
0
ファイル: Ex_1_1_18.java プロジェクト: satori/algorithms4
 public static void main(String[] args) {
   // mystery() is a product function
   assert mystery(2, 25) == 50;
   assert mystery(3, 11) == 33;
   // mysteryStar() is a power function
   assert mysteryStar(2, 5) == 32;
   assert mysteryStar(3, 4) == 81;
   StdOut.printf("mystery(2, 25) = %d\n", mystery(2, 25));
   StdOut.printf("mystery(3, 11) = %d\n", mystery(3, 11));
   StdOut.printf("mysteryStar(2, 5) == %d\n", mysteryStar(2, 5));
   StdOut.printf("mysteryStar(3, 4) == %d\n", mysteryStar(3, 4));
 }
コード例 #18
0
  /** Unit tests the <tt>Stack</tt> data type. */
  public static void main(String[] args) {
    Character[] data = {'a', 'b', 'c'};
    Stack<Character> s = new Stack<>();

    for (Character item : data) {
      s.push(item);
    }

    for (Character c : data) {
      StdOut.print(c + " ");
    }
    StdOut.println();
  }
コード例 #19
0
  public static void main(String args[]) throws IOException {

    int[] whitelist = In.readInts(args[0]);
    Arrays.sort(whitelist);
    Counter counter = new Counter("Keys");

    while (!StdIn.isEmpty()) {
      // Liest Schlüssel ein, gibt ihn aus, wenn er nicht in der Positivliste ist.
      int key = StdIn.readInt();
      if (rank(key, whitelist, counter) < 0) StdOut.println(key);
    }
    StdOut.println(counter.toString());
  }
コード例 #20
0
 public static void main(String[] args) {
   BaseballElimination division = new BaseballElimination(args[0]);
   for (String team : division.teams()) {
     if (division.isEliminated(team)) {
       StdOut.print(team + " is eliminated by the subset R = { ");
       for (String t : division.certificateOfElimination(team)) {
         StdOut.print(t + " ");
       }
       StdOut.println("}");
     } else {
       StdOut.println(team + " is not eliminated");
     }
   }
 }
コード例 #21
0
  public static void main(String[] args) {
    int n = Integer.parseInt(args[0]);
    int t = Integer.parseInt(args[1]);

    Stopwatch sw = new Stopwatch();
    PercolationStats ps = new PercolationStats(n, t);
    double secondsElapsed = sw.elapsedTime();

    StdOut.println("Seconds elapsed = " + secondsElapsed);

    StdOut.println("mean = " + ps.mean());
    StdOut.println("stddev = " + ps.stddev());
    StdOut.println("95% confidence interval = " + ps.confidenceLo() + ", " + ps.confidenceHi());
  }
コード例 #22
0
ファイル: Test.java プロジェクト: jichangfeng67/algorithm
  public static void test1() {
    int f = 0;
    int g = 1;

    for (int i = 0; i <= 0; i++) {
      f = f + g;
      g = f - g;
    }

    StdOut.println(f);
    StdOut.println(g);

    // 因为 f, g都是int类型的变量,这个函数最终导致的结果就是,不存在负数
  }
コード例 #23
0
 public static void main(String[] args) {
   int[] arr = new int[100];
   for (int i = 0; i < arr.length; i++) {
     arr[i] = i;
   }
   StdOut.println(rank(15, arr));
 }
コード例 #24
0
ファイル: demo.java プロジェクト: GaxXanh/Algorithm
 public static void main(String[] args) {
   int m = Integer.parseInt(args[0]);
   int d = Integer.parseInt(args[1]);
   int y = Integer.parseInt(args[2]);
   Date date = new Date(m, d, y);
   StdOut.println(date);
 }
コード例 #25
0
 /**
  * Test client
  *
  * @param args
  */
 public static void main(String[] args) {
   WordNet wordnet = new WordNet("/resources/synsets.txt", "/resources/hypernyms.txt");
   Outcast outcast = new Outcast(wordnet);
   In in = new In("/resources/outcast11.txt");
   String[] nouns = in.readAllStrings();
   StdOut.println("outcastX.txt: " + outcast.outcast(nouns));
 }
コード例 #26
0
ファイル: ST.java プロジェクト: Ranjodh-Singh/AlgoCustom
 /** Unit tests the <tt>ST</tt> data type. */
 public static void main(String[] args) {
   ST<String, Integer> st = new ST<String, Integer>();
   for (int i = 0; !StdIn.isEmpty(); i++) {
     String key = StdIn.readString();
     st.put(key, i);
   }
   for (String s : st.keys()) StdOut.println(s + " " + st.get(s));
 }
コード例 #27
0
ファイル: GREP.java プロジェクト: Ranjodh-Singh/AlgoCustom
 public static void main(String[] args) {
   String regexp = "(.*" + args[0] + ".*)";
   NFA nfa = new NFA(regexp);
   while (StdIn.hasNextLine()) {
     String txt = StdIn.readLine();
     if (nfa.recognizes(txt)) {
       StdOut.println(txt);
     }
   }
 }
コード例 #28
-1
  public static void main(String[] args) {
    // read the n points from a file
    In in = new In(args[0]);
    int n = in.readInt();
    Point[] points = new Point[n];
    for (int i = 0; i < n; i++) {
      int x = in.readInt();
      int y = in.readInt();
      points[i] = new Point(x, y);
    }

    // draw the points
    StdDraw.enableDoubleBuffering();
    StdDraw.setXscale(0, 32768);
    StdDraw.setYscale(0, 32768);
    for (Point p : points) {
      p.draw();
    }
    StdDraw.show();

    // print and draw the line segments
    FastCollinearPoints collinear = new FastCollinearPoints(points);
    for (LineSegment segment : collinear.segments()) {
      StdOut.println(segment);
      segment.draw();
    }
    StdDraw.show();
  }
コード例 #29
-1
  public static void main(String[] args) {
    String input = "C:/Users/gkoontz/workspace/algoAssignments/src/collinear/equidistant.txt";
    // read the N points from a file
    //	    In in = new In(args[0]);
    In in = new In(input);
    int N = in.readInt();
    Point[] points = new Point[N];
    for (int i = 0; i < N; i++) {
      int x = in.readInt();
      int y = in.readInt();
      points[i] = new Point(x, y);
    }

    // draw the points
    StdDraw.show(0);
    StdDraw.setXscale(0, 32768);
    StdDraw.setYscale(0, 32768);
    for (Point p : points) {
      p.draw();
    }
    StdDraw.show();

    // print and draw the line segments
    // BruteCollinearPoints collinear = new BruteCollinearPoints(points);
    FastCollinearPoints collinear = new FastCollinearPoints(points);
    for (LineSegment segment : collinear.segments()) {
      StdOut.println(segment);
      segment.draw();
    }
  }
コード例 #30
-1
  public static void main(String[] args) {

    // read the N points from a file
    args = new String[1];
    args[0] = "test/rs1423.txt";
    In in = new In(args[0]);
    int N = in.readInt();
    Point[] points = new Point[N];
    for (int i = 0; i < N; i++) {
      int x = in.readInt();
      int y = in.readInt();
      points[i] = new Point(x, y);
    }

    // draw the points
    StdDraw.show(0);
    StdDraw.setXscale(0, 32768);
    StdDraw.setYscale(0, 32768);
    for (Point p : points) {
      p.draw();
    }
    StdDraw.show();

    // print and draw the line segments
    FastCollinearPoints collinear = new FastCollinearPoints(points);
    for (LineSegment segment : collinear.segments()) {
      StdOut.println(segment);
      segment.draw();
    }
  }