public static void Main(string[] args) { int N = StdIn.ReadInt(); QuickFindUF uf = new QuickFindUF(N); // read in a sequence of pairs of integers (each in the range 0 to N-1), // calling find() for each pair: If the members of the pair are not already // call union() and print the pair. while (!StdIn.IsEmpty()) { int p = StdIn.ReadInt(); int q = StdIn.ReadInt(); if (uf.Connected(p, q)) { continue; } uf.Union(p, q); Console.WriteLine(p + " " + q); } Console.Write(uf.Count + " components"); }
static void Main(string[] args) { Stack <string> stack = new Stack <string>(); Queue <string> q = new Queue <string>(); Bag <string> b = new Bag <string>(); Deque <string> deque = new Deque <string>(); while (!StdIn.IsEmpty()) { string item = StdIn.ReadString(); b.Add(item); stack.Push(item); deque.PushLeft(item); if (!item.Equals("-")) { q.Enqueue(item); } //else if (!q.IsEmpty) //StdOut.Print(q.Dequeue() + " "); } stack.Reverse(); Queue <string> q2 = new Queue <string>(q); q.Reverse(); while (!q2.IsEmpty) { StdOut.Print(q2.Dequeue() + " "); } while (!q.IsEmpty) { StdOut.Print(q.Dequeue() + " "); } Stack <string> stack2 = new Stack <string>(stack); while (!stack.IsEmpty) { StdOut.Print(stack.Pop() + " "); } while (!stack2.IsEmpty) { StdOut.Print(stack2.Pop() + " "); } StdOut.Println(); while (!deque.IsEmpty) { StdOut.Print(deque.PopRight() + " "); } StdOut.Println(); foreach (var item in b) { StdOut.Print(item + " "); } }
private static void TestRank() { int[] ints = StdIn.ReadAllInts(); Array.Sort(ints); while (!StdIn.IsEmpty()) { int key = StdIn.ReadInt(); if (Rank(key, ints) < 0) { StdOut.Println(key); //如果不在名单里 则打印 } } }
public static void Main(string[] args) { int[] whitelist = new In(args[0]).ReadAllInts(); Array.Sort(whitelist); // read key; print if not in whitelist while (!StdIn.IsEmpty()) { int key = StdIn.ReadInt(); if (Rank(key, whitelist) == -1) { Console.WriteLine(key); } } }
/// <summary> /// Unit tests the <tt>Stack</tt> data type. /// </summary> /// <param name="args"></param> public static void Main(String[] args) { Stack <String> s = new Stack <String>(); while (!StdIn.IsEmpty()) { String item = StdIn.ReadString(); if (!item.Equals("-")) { s.Push(item); } else if (!s.IsEmpty()) { Console.WriteLine(s.Pop() + " "); Debug.WriteLine(s.Pop() + " "); } } Console.WriteLine("(" + s.Size + " left on stack)"); Debug.WriteLine("(" + s.Size + " left on stack)"); }
/// <summary> /// Unit tests the Queue data type. /// </summary> /// <param name="args"></param> 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()) { Console.WriteLine(q.Dequeue() + " "); Debug.WriteLine(q.Dequeue() + " "); } } Console.WriteLine("(" + q.Size + " left on queue)"); Debug.WriteLine("(" + q.Size + " left on queue)"); }