C♯の勉強

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

2013-09-24から1日間の記事一覧

TopCoder SRM582: ColorTheCells

public class ColorTheCells { public int Dfs(int[] dryingTime, int[] paintTime, int pos = 0, int used = 0, int currentTime = 0) { int n = dryingTime.Length; if (used == (1 << n) - 1) return currentTime; int minTime = int.MaxValue; for (int …

TopCoder SRM582: SpaceWarDiv2

public class SpaceWarDiv2 { public int minimalFatigue(int[] magicalGirlStrength, int[] enemyStrength, int[] enemyCount) { int n = magicalGirlStrength.Length; magicalGirlStrength = magicalGirlStrength.OrderByDescending(s => s).ToArray(); va…

TopCoder SRM582: SpaceWarDiv1

public class SpaceWarDiv1 { struct Enemy { public long strength, count; } public long minimalFatigue(int[] magicalGirlStrength, int[] enemyStrength, long[] enemyCount) { magicalGirlStrength = magicalGirlStrength.OrderByDescending(e => e).T…

TopCoder SRM582: SemiPerfectSquare

全探索 public class SemiPerfectSquare { public string check(int N) { return (from a in Enumerable.Range(1, N) from b in Enumerable.Range(1, N) where a < b && a * b * b == N select 1).Any() ? "Yes" : "No"; } }

TopCoder SRM 583: TravelOnMars

public class TravelOnMars { struct Data { public int pos; public int step; } public int minTimes(int[] range, int startCity, int endCity) { Queue<Data> qu = new Queue<Data>(); qu.Enqueue(new Data() { pos = startCity, step = 0 }); var visited = new boo</data></data>…

TopCoder SRM 583: GameOnABoard

public class GameOnABoard { static public int[] dx = { 0, 1, 0, -1 }; static public int[] dy = { 1, 0, -1, 0 }; static public void Swap<T>(ref T a, ref T b) { T t = a; a = b; b = t; } struct Point { public int row, col, step;} public int Solv</t>…

TopCoder SRM 583: IDNumberVerification

普通の実装問題。yyyy/mm/dd が一般的なカレンダーにおいて Valid かどうかは DateTime.DaysInMonth を使うと楽に書ける。 public class IDNumberVerification { const string Invalid = "Invalid"; public bool ValidBirthday(string birthday) { int year …

TopCoder SRM 583: SwappingDigits

総当りするだけ。 クエリ式の let を使ってみた 入力文字は数字だけなので、StringComparer.Ordinalで文字列比較する必要はない。しかし基本的に文字列ソートはこれを使う癖をつけたほうが良さそうなので一応付けてる。 public class SwappingDigits { stati…