publicclassSolution { publicstringFractionToDecimal(int numerator, int denominator) { } }
#解題
案例一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[TestMethod] publicvoid FractionToDecimalTest_輸入numerator_1_denominator_2() { //arrange var sut = new Solution(); var numerator = 1; var denominator = 2;
var expected = "0.5"; //act var actual = sut.FractionToDecimal(numerator, denominator);
//assert actual.Should().Be(expected); }
1 2 3 4 5 6
publicstringFractionToDecimal(int numerator, int denominator) { decimal result = Convert.ToDecimal(numerator) / Convert.ToDecimal(denominator);
return result.ToString(); }
案例二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[TestMethod] publicvoid FractionToDecimalTest_輸入numerator_2_denominator_1() { //arrange var sut = new Solution(); var numerator = 2; var denominator = 1;
var expected = "2"; //act var actual = sut.FractionToDecimal(numerator, denominator);
//assert actual.Should().Be(expected); }
直接通過測試
案例三
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[TestMethod] publicvoid FractionToDecimalTest_輸入numerator_2_denominator_3() { //arrange var sut = new Solution(); var numerator = 2; var denominator = 3;
var expected = "0.(6)"; //act var actual = sut.FractionToDecimal(numerator, denominator);
[TestMethod] publicvoid FractionToDecimalTest_輸入numerator_負2147483648_denominator_負1() { //arrange var sut = new Solution(); var numerator = -2147483648; var denominator = -1;
var expected = "2147483648"; //act var actual = sut.FractionToDecimal(numerator, denominator);
publicstringFractionToDecimal(int numerator, int denominator) { StringBuilder sb = new StringBuilder();
var _numerator = Convert.ToInt64(numerator); var _denominator = Convert.ToInt64(denominator); sb.Append(_numerator / _denominator);
//餘數 var Remainder = (_numerator % _denominator); if (Remainder == 0) { return sb.ToString(); }
sb.Append("."); var Map = new Dictionary<Int64, int>(); Map.Add(Remainder, sb.Length); while (Remainder != 0) { sb.Append((Remainder *10) / denominator); Remainder = (Remainder * 10) % denominator; if (Map.ContainsKey(Remainder)) { var Index = Map[Remainder]; //開始重複 sb.Insert(Index, "("); sb.Append(")"); break; }
Map.Add(Remainder, sb.Length); }
return sb.ToString(); }
負數問題
補上案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[TestMethod] publicvoid FractionToDecimalTest_輸入numerator_負50_denominator_8() { //arrange var sut = new Solution(); var numerator = -50; var denominator = 8; var expected = "-6.25"; //act var actual = sut.FractionToDecimal(numerator, denominator);
//只取正數 var _numerator =Math.Abs(Convert.ToInt64(numerator)); var _denominator = Math.Abs(Convert.ToInt64(denominator)); sb.Append(_numerator / _denominator);
//餘數 var Remainder = (_numerator % _denominator); if (Remainder == 0) { return sb.ToString(); }
sb.Append("."); var Map = new Dictionary<Int64, int>(); Map.Add(Remainder, sb.Length); while (Remainder != 0) { sb.Append((Remainder *10) / _denominator); Remainder = (Remainder * 10) % _denominator; if (Map.ContainsKey(Remainder)) { var Index = Map[Remainder]; //開始重複 sb.Insert(Index, "("); sb.Append(")"); break; }
Map.Add(Remainder, sb.Length); }
return sb.ToString(); }
被除數為0問題
當被除數為0時,回傳結果應為0且無關正負
補上案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[TestMethod] publicvoid FractionToDecimalTest_輸入numerator_0_denominator_負5() { //arrange var sut = new Solution(); var numerator = 0; var denominator = -5;
var expected = "0"; //act var actual = sut.FractionToDecimal(numerator, denominator);