Example #1
0
  boolean solve(int caseNum) {
    if (!in.hasNextInt()) return false;
    int n = in.nextInt();
    if (n == 0) return false;

    UFNode[] graph = new UFNode[n + 1];
    int[] yes = new int[2];
    for (int i = 0; i < graph.length; i++) graph[i] = new UFNode();

    PARSE:
    while (true) {
      switch (in.next().charAt(0)) {
        case 'c':
          graph[in.nextInt()].merge(graph[in.nextInt()]);
          break;
        case 'd':
          graph[in.nextInt()].remove();
          break;
        case 'q':
          yes[graph[in.nextInt()].find() == graph[in.nextInt()].find() ? 0 : 1]++;
          break;
        case 'e':
          break PARSE;
      }
    }

    System.out.println(yes[0] + " , " + yes[1]);

    return true;
  }
Example #2
0
  public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(new BufferedInputStream(System.in));

    int num[][] = {
      {0, 2, 3, 5, 6, 7, 8, 9},
      {2, 3, 4, 5, 6, 8, 9},
      {0, 2, 3, 5, 6, 8, 9},
      {0, 4, 5, 6, 8, 9},
      {0, 1, 2, 3, 4, 7, 8, 9},
      {0, 2, 6, 8},
      {0, 1, 3, 4, 5, 6, 7, 8, 9}
    };

    while (scanner.hasNextInt()) {
      int a = scanner.nextInt();
      String b = scanner.next();
      if (a == 0) break;
      String data[][] = new String[5 + (a - 1) * 2][(3 + (a - 1)) * b.length()];

      for (int i = 0; i < 5 + (a - 1) * 2; i++)
        for (int j = 0; j < (3 + (a - 1)) * b.length(); j++) data[i][j] = " ";

      for (int i = 0; i < b.length(); i++) {
        for (int j = 0; j < 3; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[(a + 1) * j][1 + i * (a + 2) + h] = "-";
            }
          }
        }
        for (int j = 3; j < 5; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[1 + h][(j - 3) * (a + 1) + (i * (a + 2))] = "|";
            }
          }
        }
        for (int j = 5; j < 7; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[a + 2 + h][(j - 5) * (a + 1) + (i * (a + 2))] = "|";
            }
          }
        }
      }
      StringBuffer sb = new StringBuffer("");
      for (int i = 0; i < 5 + (a - 1) * 2; i++) {
        for (int j = 0; j < (3 + (a - 1)) * b.length(); j++) {
          if ((j - (a + 1)) % (a + 2) == 0 && (j + 1) != (3 + (a - 1)) * b.length())
            sb.append(data[i][j] + " ");
          else sb.append(data[i][j]);
        }
        sb.append("\n");
      }
      System.out.println(sb);
    }
  }
Example #3
0
  public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(new BufferedInputStream(System.in));

    while (scanner.hasNextInt()) {
      int a = scanner.nextInt();
      if (a == 0) break;
      while (a / 10 != 0) {
        int sum = 0;
        while (a / 10 != 0) {
          sum = sum + a % 10;
          a = a / 10;
        }
        a = a + sum;
      }
      System.out.println(a);
    }
  }
Example #4
0
  public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(new BufferedInputStream(System.in));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int n = 1;
    while (scanner.hasNextInt()) {
      int a = scanner.nextInt();
      int b = scanner.nextInt();
      if (a == 0) break;
      BigInteger sum = BigInteger.ZERO;
      BigInteger bb = BigInteger.valueOf(b);
      for (int i = 0; i < a; i++) sum = sum.add(scanner.nextBigInteger());

      System.out.println(
          "Bill #" + n + " costs " + sum + ": each friend should pay " + sum.divide(bb) + "\n");
      n++;
    }
  }
Example #5
0
  public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(new BufferedInputStream(System.in));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    while (scanner.hasNextInt()) {
      int n = scanner.nextInt();
      int k = scanner.nextInt();

      BigInteger sum = BigInteger.ONE;

      for (int i = n; i > n - k; i--) {
        sum = sum.multiply(BigInteger.valueOf(i));
      }
      for (int i = 2; i <= k; i++) {
        sum = sum.divide(BigInteger.valueOf(i));
      }

      System.out.println(sum.toString().length());
    }
  }