Tutorial 2 Notes

1. Write a program that reads a line containing a series of integers, and writes the largest of them.

using static System.Console;

class Sum {
  static void Main() {
    int max = int.MinValue;

    foreach (string t in ReadLine().Split(' '))
      max = Max(max, int.Parse(t));

    WriteLine(max);
  }
}

2. Write a program that simulates a series of people entering a room, each of whom is assigned a random birthday. Let N be the number of people present in the room when, for the first time, two people have the same birthday. Write the number N.

using static System.Console;

class Birthday {
  static void Main() {
    Random r = new Random();
    bool[] seen = new bool[366];
    int n = 1;
    
    while (true) {
      int birthday = r.Next(366);
      if (seen[birthday])
        break;
      seen[birthday] = true;
      n += 1;
    }

    WriteLine(n);
  }
}