22/7第300位小数 (递归实现)
时间:2010-12-15 来源:chenping2008
public Int32 Result(Int32 d1, Int32 d2,Int32 nowCount,Int32 endCount)
{
if(d1<d2){
d1*=10;
}
if (nowCount == endCount)
{
return d1 / d2;
}
else
{
return Result(d1 % d2, d2, ++nowCount,endCount);
}
} 测试的代码如下: [TestMethod()]
public void ResultTest()
{
//3.14285714285714
TwentyTwoDivisionSeven target = new TwentyTwoDivisionSeven();
Assert.AreEqual(1, target.Result(22, 7, 0, 1));
Assert.AreEqual(4, target.Result(22, 7, 0, 2));
Assert.AreEqual(2, target.Result(22, 7, 0, 3));
Assert.AreEqual(8, target.Result(22, 7, 0, 4));
Assert.AreEqual(8, target.Result(22, 7, 0, 10));
Assert.AreEqual(7, target.Result(22, 7, 0, 300));
}
{
if(d1<d2){
d1*=10;
}
if (nowCount == endCount)
{
return d1 / d2;
}
else
{
return Result(d1 % d2, d2, ++nowCount,endCount);
}
} 测试的代码如下: [TestMethod()]
public void ResultTest()
{
//3.14285714285714
TwentyTwoDivisionSeven target = new TwentyTwoDivisionSeven();
Assert.AreEqual(1, target.Result(22, 7, 0, 1));
Assert.AreEqual(4, target.Result(22, 7, 0, 2));
Assert.AreEqual(2, target.Result(22, 7, 0, 3));
Assert.AreEqual(8, target.Result(22, 7, 0, 4));
Assert.AreEqual(8, target.Result(22, 7, 0, 10));
Assert.AreEqual(7, target.Result(22, 7, 0, 300));
}
相关阅读 更多 +