排班系统 c语言解法
学校实验楼有7名保安人员:钱、赵、孙、李、周、吴、陈。由于工作需要进行轮休制度,一星期中每人休息一天。预先让每一个人选择自己认为合适的休息日。请编制程序,打印轮休的所有可能方案。当然使每个人都满意,例如每人选择的休息日如下: 钱:星期一、星期六
赵:星期二、星期四
孙:星期三、星期日
李:星期五
周:星期一、星期四、星期六
吴:星期二、星期五
陈:星期三、星期六、星期日
[此贴子已经被作者于2017-3-5 22:20编辑过]
clear(); var _eWeek = ['日', '一', '二', '三', '四', '五', '六']; function initData(){ var peoples = []; peoples[0] = {cnFirstName:'钱'}; peoples[1] = {cnFirstName:'赵'}; peoples[2] = {cnFirstName:'孙'}; peoples[3] = {cnFirstName:'李'}; peoples[4] = {cnFirstName:'周'}; peoples[5] = {cnFirstName:'吴'}; peoples[6] = {cnFirstName:'陈'}; var wantsData = []; wantsData[0] = []; wantsData[0][0] = 1; wantsData[0][1] = 6; wantsData[1] = []; wantsData[1][0] = 2; wantsData[1][1] = 4; wantsData[2] = []; wantsData[2][0] = 3; wantsData[2][1] = 0; wantsData[3] = []; wantsData[3][0] = 5; wantsData[4] = []; wantsData[4][0] = 1; wantsData[4][1] = 4; wantsData[4][2] = 6; wantsData[5] = []; wantsData[5][0] = 2; wantsData[5][1] = 5; wantsData[6] = []; wantsData[6][0] = 3; wantsData[6][1] = 6; wantsData[6][2] = 0; return {wantsData:wantsData, peoples:peoples}; } function getAllTrun(peoples, wantsData){ var turn = []; var last = []; var bOver = false; var limit = 0; while (true){ if (limit++ > 1000000){ // 可去掉 break; } for (var t in wantsData){ if (typeof wantsData[t] != 'object'){ continue; } if (last[t] >= 0){ }else{ last[t] = 0; } for (var l=t; l>=0; l--){ if (wantsData[l].length <= last[l]){ last[l] = 0; if (l == 0){ bOver = true; break; }else{ last[l-1] ++; } } } } if (bOver){ break; } var unit = []; var weekStat = []; // 统计每天有几个人休息,如果限制当天不能全休息可以通过此数据判断 for (var t in wantsData){ if (typeof wantsData[t] != 'object'){ continue; } var w = wantsData[t][last[t]]; if (weekStat[_eWeek[w]] > 0) weekStat[_eWeek[w]] ++; else weekStat[_eWeek[w]] = 1; unit.push({week:w, people:t, stat:weekStat}); } last[t] ++; turn.push(unit); } return turn; } var data = initData(); var all = getAllTrun(data.peoples, data.wantsData); // test for (var a in all){ var unit = []; for (var t in all[a]){ unit.push("星期"+_eWeek[all[a][t].week]+":"+peoples[all[a][t].people].cnFirstName); } console.log(unit.join(",")); }