我有一個Range類,我希望能夠減去一組範圍。C#遞歸問題
我可以做一個罰款。
例如
Range range1 = new Range(0,100);
Range range2 = new Range(40,60);
List<Range> differences = range1.Difference(range2);
differences[0]; // 0 to 40
differences[1]; // 60 to 100
找到一個範圍,一組範圍之間的區別,當我被困在的問題是:
List<Range> rangeSet = new List<Range>();
rangeSet.Add(new Range(10, 30);
rangeSet.Add(new Range(25, 70);
rangeSet.Add(new Range(90, 120);
List<Range> results = range1.Difference(rangeSet);
結果應該是:
results[0]; // 0 to 10
results[1]; // 70 to 90
算法應該採取來自範圍和rangeSet [0]之間的差異的結果,然後將該結果用作下一次迭代的輸入等等,並最終返回結果集合的範圍。
我猜這將需要一個遞歸方法,但我不能讓我的頭繞它嗎?
「澄清」。問題比我給出的範圍例子更復雜。這是我試圖通過的測試。
[Test]
public void BandCanReturnDifferenceWithASetOfOtherBands()
{
var bandA = Band.Create<ChargeBand>("Band A");
bandA.AddMonth(EMonth.January);
bandA.AddMonth(EMonth.February);
bandA.AddDayOfWeek(DayOfWeek.Monday);
bandA.AddDayOfWeek(DayOfWeek.Tuesday);
bandA.AddTimeRange(5, 0, 11, 0);
var bandA2 = Band.Create<ChargeBand>("Band A2");
bandA2.AddMonth(EMonth.January);
bandA2.AddMonth(EMonth.December);
bandA2.AddDayOfWeek(DayOfWeek.Wednesday);
bandA2.AddDayOfWeek(DayOfWeek.Friday);
bandA2.AddTimeRange(1, 0, 10, 0);
bandA2.AddTimeRange(12, 0, 24, 0);
IList<IBand> bands = new List<IBand>();
bands.Add(bandA);
bands.Add(bandA2);
var bandB = Band.CreateAllTimesBand<ChargeBand>("Band B");
// this should result in
var bandR1 = Band.Create<ChargeBand>("R1");
var bandR2 = Band.Create<ChargeBand>("R2");
var bandR3 = Band.Create<ChargeBand>("R3");
bandR1.AddMonth(EMonth.January);
bandR1.AddMonth(EMonth.February);
bandR1.AddDayOfWeek(DayOfWeek.Monday);
bandR1.AddDayOfWeek(DayOfWeek.Tuesday);
bandR1.AddTimeRange(0, 0, 5, 0);
bandR1.AddTimeRange(11, 0, 24, 0);
bandR2.AddMonth(EMonth.January);
bandR2.AddMonth(EMonth.December);
bandR2.AddDayOfWeek(DayOfWeek.Wednesday);
bandR2.AddDayOfWeek(DayOfWeek.Thursday);
bandR2.AddTimeRange(0, 0, 1, 0);
bandR2.AddTimeRange(10, 0, 12, 0);
bandR3.AddMonth(EMonth.March);
bandR3.AddMonth(EMonth.April);
bandR3.AddMonth(EMonth.May);
bandR3.AddMonth(EMonth.June);
bandR3.AddMonth(EMonth.July);
bandR3.AddMonth(EMonth.August);
bandR3.AddMonth(EMonth.September);
bandR3.AddMonth(EMonth.October);
bandR3.AddMonth(EMonth.November);
bandR3.SetAllDays();
bandR3.AddTimeRange(0, 0, 24, 0);
// J,F,M,A,M,J,J,A,S,O,N,D - M,T,W,T,F,S,S - (00:00,24:00)
// J,F - M,T - (05:00,11:00)
// J, D - W F - (01:00,10:00),(12:00,24:00)
IList<IBand> expectedResults = new List<IBand>();
expectedResults.Add(bandR1); // J,F - M,T - (00:00,05:00),(11:00,24:00)
expectedResults.Add(bandR2); // J,D - W,F - (00:00,01:00),(10:00,12:00)
expectedResults.Add(bandR3); // M,A,M,J,J,A,S,O,N - (00:00,24:00)
var actualResults = bandB.Difference(bands);
Assert.AreEqual(expectedResults.Count, actualResults.Count);
foreach (var result in actualResults)
{
Assert.IsTrue(expectedResults.Contains(result));
}
}
對不起,如果我沒有意義,但我很難解釋。
參見:[C#遞歸問題(http://stackoverflow.com/questions/6779581/c-recursion-problem) –
@Anthony你忘記了一個基本案例嗎?我得到堆棧溢出。 – dlev
你可以展示你現在使用的算法嗎?我並不關注你如何確定第一套答案,更不用說你的第二個答案,以幫助其他人解決問題。 – DRapp