c#算法例子
1,计算几个区间是否重叠
有几组数据,满足以下条件:
每组数据有最大和最小值,且必须有一个最大或一个最小
var tempNum = 0.001m;
var maxNum = request.AutoOpenOrderCondition.Max(s => s.MaxPurchasePrice ?? 0) + tempNum;
var minNum = request.AutoOpenOrderCondition.Max(s => s.MinPurchasePrice ?? 0) - tempNum;
var qujian = request.AutoOpenOrderCondition.Select(s => new Tuple<decimal, decimal>(HandleValueByCompareType(s.MinPurchasePriceCompareType, s.MinPurchasePrice ?? minNum, tempNum), HandleValueByCompareType(s.MaxPurchasePriceCompareType, s.MaxPurchasePrice ?? maxNum, -tempNum))).ToList();
for (int i = 0; i < qujian.Count - 1; i++)
{
var itema = qujian[i];
for (int j = i + 1; j < qujian.Count; j++)
{
var itemb = qujian[j];
if (!(itema.Item2 < itemb.Item1 || itema.Item1 > itemb.Item2))
{
throw CommonFaultFactory.CreateFault("自动开单条件采购单价重叠", "SaveCentralizedPurchasingSetOpenOrderRules");
}
}
}
decimal HandleValueByCompareType(string compareType, decimal value, decimal t = 0.001m)
{
switch (compareType)
{
case "<":
return value + t;
case "<=": return value;
}
return value;
}