[英]Generating a list containing range of numbers from min and max values
我正在嘗試生成一個列表,該列表包含給定的最小值和最大值的數字范圍。
如果我的最小值是14500,最大值是58000,則需要該方法返回;
少於盧比。 20000
盧比 20,001-Rs。 30,000
盧比 30,001-Rs。 40,000
盧比 40,001-Rs。 50,001
盧比 50,001及以上
我的問題類似於此php問題 ,但如果可能,我想使用LINQ
。
我已經嘗試了Enumerable.Range
和一些使用for
和while
代碼。 最好的方法是什么?
這可能是錯誤的,因為我是在記事本中編寫的,但是您會明白的:
var start = min - min % 10000 + 10000;
list.Add(string.Format("less then {0}", start));
while(start < max)
{
if(start + 10000 > max)
list.Add(string.Format("{0} and above", start + 1));
else
list.Add(string.Format("rs. {0} - rs. {1}", start + 1, start + 10000));
start += 10000;
}
該程序將僅考慮最大值,因為您說的是您可以接受6個以下的價格范圍。
class Program
{
static void Main(string[] args)
{
int maxItems = 6;
int minItems = 3;
int maxValue = 99232;
int minValue = 99000;
//the list to store string formmated items
List<string> finalList = new List<string>();
int divider = (GetBestValue(maxValue, minValue, maxItems, minItems));
int startingPoint = ((minValue / divider) + 1) * divider;
finalList.Add(string.Format("less then {0}", startingPoint));
int currentAmmount = startingPoint;
while (currentAmmount < maxValue)
{
if (currentAmmount + divider > maxValue)
finalList.Add(string.Format("{0} and above", currentAmmount + 1));
else
finalList.Add(string.Format("rs. {0} - rs. {1}", currentAmmount + 1, currentAmmount + divider));
currentAmmount += divider;
}
foreach (var item in finalList)
{
Console.WriteLine(item);
}
}
/// <summary>
/// This method will seek the best divider to take
/// </summary>
/// <param name="max">Max value to use</param>
/// <param name="maxItems"></param>
/// <param name="minValue"></param>
/// <returns></returns>
static int GetBestValue(int max, int min, int maxItems, int minItems)
{
int currentDivider = 1;
int currentItems;
int startingMaxValue = max;
int range = (max - min);
while (startingMaxValue/10 > 0)
{
currentDivider *= 10;
startingMaxValue /= 10;
}
//check aginst max value
while (max / currentDivider > maxItems)
{
currentDivider *= 2;
}
//check aginst min items
currentItems =range / currentDivider;
while (currentItems < minItems)
{
currentDivider /= 2;
currentItems = range / currentDivider;
}
return currentDivider;
}
}
如果要考慮最小值,則必須提供一些如何將范圍划分的邏輯
編輯:請注意,此答案返回范圍內的所有數字。 可能不需要。 對於那些正在尋找其他答案的人,我將在這里保留該答案。
我沒有使用LINQ。 但是,此方法允許您對數字進行分區。 它從min到第一個分區,如:min = 1563,分區= 100,然后第一個列表是1563至1600。下一個列表是1601至1700。
static void Main(string[] args)
{
foreach (var numberRange in NumberRanges(14500, 58000, 10000))
{
Console.WriteLine(": {0}, {1}", numberRange.Min(), numberRange.Max());
}
}
static IEnumerable<IEnumerable<int>> NumberRanges(int min, int max, int partitionSize)
{
int num = min;
List<int> numPart = new List<int>(partitionSize);
while (num <= max)
{
numPart.Add(num++);
if (num % partitionSize == 0)
{
numPart.Add(num++);
yield return numPart;
numPart.Clear();
}
}
if (numPart.Any())
yield return numPart;
}
通過執行以下操作,您最多只能強制6個列表:
NumberRanges(14500, 100000, 10000).Take(6);
給予:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.