Home  >  Q&A  >  body text

php - 有48个1与0的字符串代表1天的时间, 半小时为单位

需求是 如 当天的 00:30 - 11:00 转为仅有1与0的字符串, 其中 1 代表包含在时间段内, 0代表不包含

得出 00:30 - 11:00 的字符串为 011111111111111111111110000000000000000000000000

时间可能是散开分布, 不一定是连贯的. 如 11:00-12:00 14:00-16:00.

求解应该怎么做.谢谢各位大佬~

阿神阿神2737 days ago484

reply all(3)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-16 13:05:25

    Written a js version for the console

    let calcPos = arr=>arr[0]*2+(arr[1]===0?0:1);
    ([[0,30],[11,0]].reduce(
        (a,b,i,arr)=>
            a+= new Array(calcPos(b)+(i===0||i%2===0?0:1)-a.length)
                .fill(i%2===0?"0":"1")
                .join("")
        , "")+new Array(48).fill("0").join(""))
        .slice(0,48);

    Time is used[[0,30],[11,0]]表示00:30 - 11:00, so the second situation is like this

    ([[0,30],[11,0],[14,0],[16,0]].reduce((a,b,i,arr)=>a+=new Array(calcPos(b)+(i===0||i%2===0?0:1)-a.length).fill(i%2===0?"0":"1").join(""), "")+new Array(48).fill("0").join("")).slice(0,48);

    The following is the decoding

    ("0"+"011111111111111111111110000011111000000000000000")
        .split("")
        .reduce(
            (a,b,i,arr)=>
                i===0?a:b!==arr[i-1]?[...a, (b==="1"?i:(i-1)) -1]:a
            , [])
        .map(i=>[(i-(i%2===0?0:1))/2, i%2===0?0:30]);

    reply
    0
  • 黄舟

    黄舟2017-05-16 13:05:25

    Convert the time to seconds, and it’s OK to change the seconds to binary. As for - whether you can use the binary format according to the coding table or other methods, it’s up to you

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 13:05:25

    Isn’t the half-hour unit dividing 1 day into 48 segments? 0-47 means that the segment number corresponds to each period of time.
    The i-th string of string corresponds to the segment number i. 0 means that it is during this time. 1 means that it is not during this time.
    The specific program is not difficult to write.
    Just ask for the segment number and replace the string content.
    The segment number is to convert the time into hours and then divide it by 0.5 to round up.

    reply
    0
  • Cancelreply