连续座算法
连续座算法
新建模板
声明算法
/// <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 initSeatNoFlog = 0; //初始连续座位号下标 var initSeatNoSubscript = 0; //记录连续次数 var recordConsecutiveTimes = 0; for (int i = 0; i < dataSource.Length; i++) { var temp = dataSource[i] - i; if (temp == initSeatNoFlog) { recordConsecutiveTimes++; } else { initSeatNoFlog = 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(); }
相关推荐
湾区人工智能 2020-11-20
Pokemogo 2020-11-16
baijingjing 2020-11-16
baijingjing 2020-11-15
Site 2020-11-07
lwnylslwnyls 2020-11-06
justaipanda 2020-11-05
MachineIntellect 2020-11-02
xueyuediana 2020-10-30
GeraldJones 2020-10-30
Tips 2020-10-29
baijingjing 2020-10-28
baijingjing 2020-10-27
硕鼠 2020-10-26
playoffs 2020-10-26
scuyxi 2020-10-25
playoffs 2020-10-25
yise001 2020-10-23