连续座算法
声明算法
/// <summary>
/// 判断是否存在指定座数量的连续座(算法)
/// </summary>
/// <param name="num">座位数量</param>
/// <param name="dataSource">数据源</param>
/// <param name="seatNos">符合条件的连续座位号</param>
/// <returns></returns>
private static bool IsExistContinuity(int num,int[] dataSource,ref int[] seatNos)
{
//初始连续座位号标识
var initSeatNoFlag = 0;
//初始连续座位号下标
var initSeatNoSubscript = 0;
//记录连续次数
var recordConsecutiveTimes = 0;
for (int i = 0; i < dataSource.Length; i++)
{
var temp = dataSource[i] - i;
if (temp == initSeatNoFlag)
{
recordConsecutiveTimes++;
}
else
{
initSeatNoFlag = temp;
initSeatNoSubscript = i;
recordConsecutiveTimes = 1;
}
if (recordConsecutiveTimes != num) continue;
Array.Copy(dataSource,initSeatNoSubscript,seatNos,0,num);
return true;
}
return false;
}
调用
static void Main(string[] args)
{
//座位数据源
var dataSource = new [] {1,3,4,5,7,9,10,17};
var seatNos = new int[3];
var result = IsExistContinuity(3, dataSource,ref seatNos);
Console.WriteLine(result);
Console.ReadKey();
}