http://cyber-dojo.org/ 是一个很棒的地方,有兴趣的朋友可以去看一下。
贴一下我的第一次实践经历,大约历时三个小时左右,没有了VS的辅助,写C#还真的有点不顺手,中间黄色的都是代码报错占的比例比较高。
有图有真像:
题目:
100 doors in a row are all initially closed. You make 100 passes by the doors. The first time through, you visit every door and toggle the door (if the door is closed, you open it; if it is open, you close it). The second time you only visit every 2nd door (door #2, #4, #6, ...). The third time, every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th door. Question: What state are the doors in after the last pass? Which are open, which are closed? [Source http://rosettacode.org]
代码:
using System.Collections.Generic; using System.Text; using System; public class DoorGame { private const int doorsCount = 100; public static string Play(int count) { string doors = InitDoorsStatus(); char[] tmp = doors.ToCharArray(); for(int i=1; i < count + 1; i++) { for(int j = 1; j<doorsCount + 1; j ++) { int k = j; if (i > 1){ if (j % i == 0 ) { k = j ; } else { continue ; } } if (k > doorsCount){ break; } tmp [k - 1] = tmp [k - 1] == '0' ? '1':'0'; } //if (i > 90) {Console.WriteLine(new string(tmp));} } doors = new string(tmp); //Console.WriteLine(doors); return doors; } private static string InitDoorsStatus() { string _r = string.Empty; StringBuilder sb = new StringBuilder(); for(int i= 0; i < doorsCount; i++) { sb.Append("0"); } _r = sb.ToString(); return _r; } }
测试代码
using NUnit.Framework; using System.Collections.Generic; using System.Text; [TestFixture] public class DoorGameTest { [Test] public void shold_be_all_doors_closed_init() { string _t = "0000000000" + "0000000000"+ "0000000000" + "0000000000"+ "0000000000" + "0000000000"+ "0000000000" + "0000000000"+ "0000000000" + "0000000000"; Assert.AreEqual(true , _t.Equals(DoorGame.Play(0))); } [Test] public void shold_be_all_doors_play_1() { string _t = "1111111111" + "1111111111"+ "1111111111" + "1111111111"+ "1111111111" + "1111111111"+ "1111111111" + "1111111111"+ "1111111111" + "1111111111"; Assert.AreEqual(true , _t.Equals(DoorGame.Play(1))); } [Test] public void shold_be_all_doors_play_2() { string _t = "1010101010" + "1010101010"+ "1010101010" + "1010101010"+ "1010101010" + "1010101010"+ "1010101010" + "1010101010"+ "1010101010" + "1010101010"; Assert.AreEqual(true , _t.Equals(DoorGame.Play(2))); } [Test] public void shold_be_all_doors_play_3() { string _t = "1000111000" + "1110001110"+ "0011100011" + "1000111000"+ "1110001110" + "0011100011"+ "1000111000" + "1110001110"+ "0011100011" + "1000111000"; Assert.AreEqual(true , _t.Equals(DoorGame.Play(3))); } [Test] public void shold_be_all_doors_play_100() { string _t = "1001000010" + "0000010000"+ "0000100000" + "0000010000"+ "0000000010" + "0000000000"+ "0001000000" + "0000000000"+ "1000000000" + "0000000001"; Assert.AreEqual(true , _t.Equals(DoorGame.Play(100))); } }
测试结果:
ProcessModel: Default DomainUsage: Single Execution Runtime: mono-4.0 ..... Tests run: 5, Errors: 0, Failures: 0, Inconclusive: 0, Time: 0.024756 seconds Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0