码迷,mamicode.com
首页 > 其他好文 > 详细

dji

时间:2017-08-30 20:51:20      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:interface   short   create   ice   roc   return   var   proc   foreach   

2/3

var readline = require(‘readline‘);
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

var n = 0,
    brr = [];
rl.on(‘line‘, function(line) {
    if (!n) {
        n = parseInt(line)
    } else {
        brr.push(line);
        var res = [];
        if (brr.length == n) {
            for (var i = 0; i < n; i++) {
                var lines = brr[i].split(‘ ‘);
                var obj = {
                    date: lines[0],
                    time: lines[1]
                }
                res.push(obj)
            }

            res.sort(function(a, b) {
                return a.date - b.date
            })

            var obj = {};
            for (var i = 0; i < res.length; i++) {
                var k = res[i].date
                if (obj[k]) {
                    obj[k] = obj[k].concat(‘&‘ + res[i].time)
                } else {
                    obj[k] = res[i].time;
                }
            }

            for (var k in obj) {
                obj[k] = obj[k].split(‘&‘).sort(function(a, b) {
                    return a.split(‘:‘).join(‘‘) - b.split(‘:‘).join(‘‘);
                })
            }

            var arr = [];
            for (var k in obj) {
                obj[k].forEach(function(x) {
                    var o = {
                        date: k,
                        time: x
                    }
                    arr.push(o)
                })
            }

            var ans = [];
            for (var j = 0; j < arr.length - 1; j += 2) {
                var d1 = arr[j].date,
                    d2 = arr[j + 1].date;
                if (d1 == d2) {
                    var t1 = arr[j].time,
                        t2 = arr[j + 1].time;

                    if (del(t1) < del(‘03:00:00‘) && del(t2) > del(‘03:00:00‘)) {
                        continue;
                    }

                    if (del(t1) >= del(‘03:00:00‘)) {
                        if (del(t1) <= del(‘12:30:00‘)) {
                            if (del(t2) <= del(‘12:30:00‘)) {
                                ans.push(d1 + ‘ ‘ + seconds(t1, t2));
                                continue;
                            } else {
                                if (del(t2) >= del("14:00:00")) {
                                    ans.push(d1 + ‘ ‘ + (seconds(t1, t2) - seconds(‘12:30:00‘, ‘14:00:00‘)));
                                    continue;
                                } else {
                                    ans.push(d1 + ‘ ‘ + seconds(t1, ‘12:29:59‘));
                                    continue;
                                }

                            }
                        } else if (del(t1) >= del("14:00:00")) {
                            ans.push(d1 + ‘ ‘ + seconds(t1, t2));
                            continue;
                        }

                    } else {
                        if (del(t2) < del("03:00:00")) {
                            ans.push(short(d1) + ‘ ‘ + seconds(t1, t2));
                            continue;
                        }
                    }
                } else {
                    var t1 = arr[j].time,
                        t2 = arr[j + 1].time;
                    if (del(t1) > del(‘14:00:00‘) && del(t2) < del(‘02:59:59‘)) {
                        ans.push(d1 + ‘ ‘ + (seconds(t1, ‘23:59:60‘) + seconds(‘00:00:00‘, t2)));
                        continue;
                    }
                }
            }

            ans.forEach(function(x) {
                console.log(x)
            })


            //清空
            brr = [];
            n = 0;
        }
    }
});



function del(s) {
    return s.split(‘:‘).join(‘‘);
}

function seconds(t1, t2) {
    var times1 = t1.split(":"),
        h1 = parseInt(times1[0]),
        m1 = parseInt(times1[1]),
        s1 = parseInt(times1[2]),
        times2 = t2.split(‘:‘),
        h2 = parseInt(times2[0]),
        m2 = parseInt(times2[1]),
        s2 = parseInt(times2[2]);

    return (h2 - h1) * 60 * 60 + (m2 - m1) * 60 + (s2 - s1)
}

function short(d1) {
    var n = d1.split(‘.‘)[1] - 1;
    var s = ‘0‘ + n;
    return d1.slice(0, 3).concat(s.slice(-2));
}
var readline = require(‘readline‘);
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

var n = 0,
    arr = [];
rl.on(‘line‘, function(line) {
    if (!n) {
        n = parseInt(line)
    } else {
        arr.push(line);
        if (arr.length == n) {
            var res = [];

            arr.forEach(function(x) {
                res.push(count(x));
            })

            res.forEach(function(x) {
                console.log(x);
            })
            //清空
            arr = [];
            n = 0;
        }

    }

});

function count(s) {
    var n = 0;
    var ans = [];
    for (var i = 0; i < s.length; i++) {
        if (s[i] == ‘j‘) {
            ans.push(i);
        }
    }


    ans.forEach(function(i) {
        var forward = s.slice(0, i),
            backword = s.slice(i + 1);
        var d = countchar(forward, ‘d‘);
        var i = countchar(backword, ‘i‘);
        n += d * i
    })
    return n;
}


function countchar(s, char) {
    var n = 0;
    for (var i = 0; i < s.length; i++) {
        if (s[i] == char) {
            n++;
        }
    }

    return n;
}

 

dji

标签:interface   short   create   ice   roc   return   var   proc   foreach   

原文地址:http://www.cnblogs.com/t1amo/p/7455047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!