Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2015年1月30日 D6 让题目继续吧! #39

Open
think2011 opened this issue Jan 30, 2015 · 8 comments
Open

2015年1月30日 D6 让题目继续吧! #39

think2011 opened this issue Jan 30, 2015 · 8 comments

Comments

@think2011
Copy link
Collaborator

做了20年前端开发,你终于领悟赚钱不能靠打工,于是你辞职回家开了间小饭店。

你的日常是这样的,平时客人不多,就安排3个人一张桌子。

var guests = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(repast(guests, 3)); // 结果:[[1,2,3], [4,5,6], [7,8,9]];

忙碌时,就安排5个人一张桌子。

var guests = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
console.log(repast(guests, 5)); // 结果:[[1,2,3,4,5], [6,7,8,9,10], [11,12]];

作为前资深前端开发,这点问题根本难不住你:

function repast (guests, seatNum) {
// 请给客人合理的分配座位
}
@xinglie
Copy link

xinglie commented Jan 30, 2015

function repast (guests, seatNum) {
 var a=[];
 while(guests.length){a.push(guests.splice(0,seatNum));}
 return a;
}

@think2011
Copy link
Collaborator Author

@xinglie 好快,通过。

@Edward1992
Copy link

function repast(guests, seatNum){
    var result = [];

    var fullSeats = Math.floor(guests.length / seatNum);
    var remainGuests = guests.length % seatNum;

    function repastHelper(seatIndex){
        if(seatIndex == fullSeats){
            if(remainGuests > 0){
                result.push(guests.slice(seatIndex*seatNum));
            }
        }
        else{
            result.push(guests.slice(seatIndex*seatNum, (seatIndex + 1)* seatNum));
            repastHelper(seatIndex + 1);
        }
    }

    repastHelper(0);
    return result;
}

function test(){
    var guests;

    guests = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    console.log(repast(guests, 3));

    guests = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
    console.log(repast(guests, 5));

    guests = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];
    console.log(repast(guests, 4));
}

test();

@VaJoy
Copy link

VaJoy commented Feb 27, 2015

如果想保留guests数组不被修改的话,可以这样

function repast (guests, seatNum) {
    for(var i=0,l=guests.length/seatNum|0,a=[],temp;i<l+1;){
        if((temp = guests.slice(seatNum*i,seatNum*(++i))).length)
        a.push(temp)
    }
    return a;
}

@jsers
Copy link

jsers commented Feb 27, 2015

function repast(guests,num){
    var a = [];
    for(var i=0;i<Math.ceil(guests.length/num)*num;i=i+num){
        a.push(guests.slice(i,i+num))
    }
    return a
}

@soulcm
Copy link

soulcm commented Mar 5, 2015

function repast(guests, seatNum) {
    var res = [];
    for (var i = 0; i < guests.length; i = i + seatNum) {
        res.push(guests.slice(i, i + seatNum));
    }
    return res;
}

@XadillaX
Copy link
Collaborator

XadillaX commented Mar 5, 2015

好久没来了,最近忙成翔。

function repast (guests, seatNum) {
    return guests.reduce(function(res, cur) {
        return ((!res.length || res[res.length - 1].length >= seatNum) ?
            (res.push([]), res[res.length - 1].push(cur)) :
            res[res.length - 1].push(cur)),
            res;
    }, []);
}

@Xinuy-Leung
Copy link

function repast (guests, seatNum) {
    var arr=[];
    for(var i=0;i<guests.length;i+=seatNum){
        arr.push(guests.slice(i,i+seatNum));
    }
    return arr;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants