这是我如何做的一个例子,因为从最初的问题来看,不清楚结果是否应该是一个字符串、一个数字数组或其他什么。
class Program {
static void Main(string[] args) {
int[] numbers = { 49, 78, 135, 245, 0, 1, 50, 51, 100, 101, 150, 151, 200, 201, 300, 301, 400};
foreach(var number in numbers) {
string result = GetRange(number);
Console.WriteLine(string.Format("Input: {0}\tResult: {1}",number, result));
}
Console.ReadKey();
}
private static string GetRange(int number) {
if(number <= 50) {
return RangeByQuotient(number, 10);
} else if(number <= 100) {
return RangeByQuotient(number, 25);
} else if(number <= 200) {
return RangeByQuotient(number, 50);
} else if(number <= 400) {
return RangeByQuotient(number, 100);
}
return "Invalid Range";
}
private static string RangeByQuotient(int number, int quotient) {
var lower = ((number-1) / quotient) * quotient + 1;
var upper = (((number-1) / quotient)+1) * quotient;
return string.Format("({0}-{1})", lower, upper);
}
}
结果是
Input: 49 Result: (41-50)
Input: 78 Result: (76-100)
Input: 135 Result: (101-150)
Input: 245 Result: (201-300)
Input: 0 Result: (1-10)
Input: 1 Result: (1-10)
Input: 50 Result: (41-50)
Input: 51 Result: (51-75)
Input: 100 Result: (76-100)
Input: 101 Result: (101-150)
Input: 150 Result: (101-150)
Input: 151 Result: (151-200)
Input: 200 Result: (151-200)
Input: 201 Result: (201-300)
Input: 300 Result: (201-300)
Input: 301 Result: (301-400)
Input: 400 Result: (301-400)
更新:
更新以正确处理边界情况,如 50、51 等。