C♯の勉強

C♯4.0 で TopCoder の過去問を解きます。

TopCoder SRM603: MiddleCode

問題文にかかれている通りに実装するだけ。

public class MiddleCode {
    public string encode(string s) {
        if (s.Length == 0) return "";
        int removePosition;
        if (s.Length % 2 == 1) {
            removePosition = s.Length / 2;
        } else {
            if (s[s.Length / 2 - 1] < s[s.Length / 2]) {
                removePosition = s.Length / 2 - 1;
            } else {
                removePosition = s.Length / 2 ;
            }
        }
        return s[removePosition] + encode(s.Remove(removePosition, 1));
    }
}