You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

按摩预约APP如何在服务商间均匀分配含余数的30分钟时段

按摩预约APP服务商时段分配实现问题

我开发了一款按摩预约APP,用户可预约多段服务,服务时长以30分钟为单位,范围在30分钟至2小时之间。

名词定义

  • Vendor:提供按摩服务的服务商
  • Client:接受服务的客户

业务规则

  • 每名服务商最多承接4名客户,客户可选择多名服务商为多人提供服务
  • 预约时可设置单人服务时长(分钟)、服务人数、服务商数量三个参数
  • 每名服务商最多可分配4小时的30分钟时段

实现需求

需根据传入的单人服务时长、服务人数参数,将30分钟时段均匀分配给各服务商,同时处理好余数问题。现有实现仅适配部分场景,代码内置了多组测试示例。

现有实现代码

// 测试示例说明
// 正常场景:3个服务商各2小时,1个服务商3小时
// const lengthInMinutes = 60
// const hours = lengthInMinutes / 60;
/* const numberOfClients = 7
const numberOfVendors = 3 */

// 正常场景:3个服务商各2小时
// const lengthInMinutes = 60
// const hours = lengthInMinutes / 60;
/* const numberOfClients = 6
const numberOfVendors = 3 */

// 正常场景:2个服务商各2小时,1个服务商1小时
// const lengthInMinutes = 60
// const hours = lengthInMinutes / 60;
/* const numberOfClients = 5
const numberOfVendors = 3 */

// 正常场景:1个服务商4小时
// const lengthInMinutes = 60
// const hours = lengthInMinutes / 60;
/* const numberOfClients = 4
const numberOfVendors = 1 */

// 正常场景:3个服务商各1小时,1个服务商2小时
// const lengthInMinutes = 60
// const hours = lengthInMinutes / 60;
/* const numberOfClients = 4
const numberOfVendors = 3 */

// 异常场景:预期4个服务商各2小时,2个服务商各1小时,当前代码计算结果不符合预期
const lengthInMinutes = 60
const hours = lengthInMinutes / 60;
const numberOfClients = 10
const numberOfVendors = 6


let totalHours = numberOfClients * hours;
console.log("totalHours: " + totalHours)

let hoursPerVendor = Math.round(totalHours / numberOfVendors);
let hoursRemainder = (totalHours / numberOfVendors) % 1;

let vendorHours = new Array(numberOfVendors).fill(0).map((i) => {
    return hoursPerVendor
})

console.log("Hours remainder: " + hoursRemainder)
if (hoursRemainder !== 0.0) {
  if (hoursRemainder > 0.5) {
    // 最后一个服务商减1小时
    vendorHours[vendorHours.length - 1] = vendorHours[vendorHours.length - 1] - 1
  }
  if (hoursRemainder < 0.5) {
    // 最后一个服务商加1小时
    vendorHours[vendorHours.length - 1] = vendorHours[vendorHours.length - 1] + 1
  }
}

console.log(vendorHours)

内容的提问来源于stack exchange,提问作者Oliver Dixon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 22:57:03