3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

1時間以内のアレの5をC#で

Posted at

元ネタ「1時間以内に解けなければプログラマ失格となってしまう5つの問題が話題に

sample.cs
tatic void Main(string[] args)
{
    var sources = "123456789";
    var comm = " +-";
 
    var insertPoint = sources.Length - 1;
    var commCount = comm.Length;
 
    const int value = 100;
 
    System.Data.DataTable dt = new System.Data.DataTable();
 
    foreach (var v in Enumerable.Range(0, (int)Math.Pow(commCount, insertPoint)))
    {
        var formula = sources;
        foreach (var n in Enumerable.Range(0, insertPoint).Reverse())
        {
            var index = (v / (int)Math.Pow(commCount, n)) % commCount;
            formula = (index == 0) ? formula : formula.Insert(n + 1, new string(new[] { comm.ElementAt(index) }));
        }
 
        var result = (int)dt.Compute(formula, "");
        if (result == value)
            Console.WriteLine(formula + " = " + result);
    }
}
  • DataTableを使いたかっただけだろ感があります。
  • commに他の演算子を追加できたりなんかもします。
  • 組み合わせを出してるところが読みにくいですし、あっという間にint値がオーバーフローしそうです。
  • 名前の付け方って難しいですね。
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?