Mozy Contest Answers

Bryan Sant bryan.sant at gmail.com
Sat Nov 4 11:51:08 MST 2006


Round 1 Answers in Java:
------------------------------------
Use Java 5
Compile with:  javac -g:none Q.java
Execute with: java -Xverify:none Q
------------------------------------

import java.io.*;
public class Q1 {
  public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
    String line;
    while ((line = reader.readLine()) != null) {
      int openCount = 0;
      int pairCount = 0;
      for (char c : line.toCharArray()) {
        if (c == '(')
          openCount++;
        if (c == ')') {
          if (openCount > pairCount)
            pairCount = openCount;
          openCount--;
        }
      }
      System.out.println(pairCount);
    }
  }
}


import java.io.*;
import java.util.*;
public class Q2 {
  public static void main(String[] args) throws Exception {
    List<Integer> nums = new ArrayList<Integer>();
    BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
    String line;
    while ((line = reader.readLine()) != null) {
      int roof = Integer.parseInt(line);
      for (int i = 7; i < roof; i += 7) {
        StringBuilder sb = new StringBuilder();
        sb.append(i);
        int rev = Integer.parseInt(sb.reverse().toString());
        if (rev % 7 == 0) {
          nums.add(i);
        }
      }
      Collections.reverse(nums);
      for (int i : nums) {
        System.out.println(i);
      }
    }
  }
}

import java.io.*;
import java.util.*;
public class Q3 {
  public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
    String line;
    while ((line = reader.readLine()) != null) {
      List<Integer> nums = new ArrayList<Integer>();
      for (char c : line.toCharArray()) {
        if (Character.isDigit(c)) {
          nums.add(Integer.parseInt("" + c));
        }
      }
      int total = 0;
      for (int i : nums) {
        total += i;
      }
      System.out.println(total);
    }
  }
}

import java.io.*;
public class Q4 {
  public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
    String matchMe = "mozy";
    int matches = 0;
    String line;
    while ((line = reader.readLine()) != null) {
      int matchIndex = 0;
      for (char c : line.toCharArray()) {
        if (c == matchMe.charAt(matchIndex))
          matchIndex++;
        if (matchIndex == matchMe.length()) {
          matches++;
          break;
        }
      }
    }
    System.out.println(matches);
  }
}

import java.io.BufferedInputStream;
import java.net.Socket;
public class Q5 {
  public static void main(String[] args) throws Exception {
    Socket s = new Socket("mozy.com", 8787);
    BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
    bis.skip(13485);
    char c = (char) bis.read();
    System.out.println(c);
  }
}

import java.io.*;
public class Q6 {
  public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
    String line;
    while ((line = reader.readLine()) != null) {
      for (char c : line.toCharArray()) {
        if (!Character.isDigit(c)) {
          c += 1;
          System.out.print(c);
        }
      }
      System.out.println();
    }
  }
}

-Bryan



More information about the PLUG mailing list