3

我有一个问题,我试图计算有多少孩子的年龄(作为关键)如下,但我们希望得到基于孩子的大孩子组(在这种情况下是 4 和 2)> = 4 个孩子组我想获得可以组成一个 bigkids 组的最大年龄数。

问题我怎样才能得到最大年龄小于/等于 4 个孩子的新创建组的年龄?

所以这是我们输入的内容

max_bigest_kids_in_group = 4
kids object = {
//age 0 have 7 kids
'0' : 7
//age 1 have 3 kids
'1' : 3
//age 2 have 2 kids
'2' : 2 
//age 4 have 1 kids
'4' : 1
//lets say there is no age 3 or 5,6 and so on.
}

这是我目前在psudo中尝试过和目前所做的

try    biggest  next biggest 
    ('4' = 1) + ('2' = 2)  (oke becouse 1+2 is 3 >= 4) save array [4 and 2]
try    biggest  next biggest next biggest
    ('4' = 1) + ('2' = 2) + ('1' = 3) (not oke becouse 1+2+3 is 6 >= 4) stop becouse its more then x = 4
    get array [4 and 2] 

    // expected result is {'4','2'} or ['4','2'] so if max_bigest_kids_in_group = 6 then it will be ['4','2','1']

我们怎样才能得到预期的结果?在javascript中

附言。对不起,如果标题不是那么准确,如果有任何建议,请评论或编辑。

4

3 回答 3

2

我假设您的对象kids默认情况下未排序。此外,在 JS 中,您不能依赖for...in循环返回属性的顺序(有关详细信息,请参阅链接)。因此,第一步是将对象转换为可以排序的结构,并确保获得的顺序是正确的:

kids  = {
    //age 0 have 7 kids
    '0' : 7,
    //age 1 have 3 kids
    '1' : 3,
    //age 2 have 2 kids
    '2' : 2,
    //age 4 have 1 kids
    '4' : 1
    //lets say there is no age 3 or 5,6 and so on.
}

var array = [];

for (var age in kids) {
    if (kids.hasOwnProperty(age)) {
        array.push({age: age, count: kids[age]});
    }
}

拥有它之后,您可以按如下方式对数组进行排序:

array.sort(function(a, b) { return a.count - b.count });

然后,您可以进行计算:

var maxKids = 4;
var groups = [];
var count = 0;

for (var i = 0; i < array.length; i++) {
    count += array[i].count;

    if (count <= maxKids)
        groups.push(array[i].age);
    else
        break;
}

console.log(groups);

此方法还涵盖了所有孩子组何时超过您可以拥有的最大孩子数,因此您将获得一个空数组。

于 2012-07-10T18:32:42.970 回答
2

这很简单。就像在你的伪代码中一样,对孩子们进行循环。

var kids = {
   '0' : 7, //age 0 have 7 kids
   '1' : 3, //age 1 have 3 kids
   '2' : 2, //age 2 have 2 kids
   '4' : 1  //age 4 have 1 kids
};
function getAgesOfOldestKids(n) {
/* get: {number} how many kids */

    // lets begin with some magic to find out the maximum age in the set
    // you might code this with a for-in-loop over kids
    var max = Math.max.apply(null, Object.keys(kids));

    var ages = []; // ages of the kids in the result
    var count = 0; // how many kids are in the set
    for (var i=max; i>=0; i--)
        if (i in kids) { // age level exists
            count += kids[i]; // add number of kids
            if (count > n) // kids in the set are more than allowed
                return ages; // break the loop
            else
                ages.push(i); // add this age step to the result
        }
    return ages; // there may be less children than requested
}

> getAgesOfOldestKids(4)
["4", "2"]

// a trick to get the number of kids in the result:
> var ages = [4, 2];
> ages.reduce(function(n, age){return n+kids[age]}, 0)
3

其他可能且更短的解决方案,直接在孩子的钥匙上循环:

function getAgesOfOldestKids(n) {
/* get: {number} how many kids */

    // ages, sorted descending
    var ages = Object.keys(kids).sort(function(a,b){return b-a;});

    var count = 0; // how many kids are in the set
    for (var i=0; i<ages.length-1 && count <= n; i++)
        count += kids[ages[i]]; // add number of kids
    return ages.slice(0, i-1);
}
于 2012-07-10T17:47:10.467 回答
0

不确定我是否完全遵循了这个问题,但是,它是沿着这些方向吗?

http://jsfiddle.net/kyuff/Tkj2r/7/

var kids = {
//age 0 have 7 kids
    0 : 7,
//age 1 have 3 kids
    1 : 3,
//age 2 have 2 kids
    2 : 2,
//age 4 have 1 kids
    4 : 1
    };
function between(min, max) {
    var age = 0;
    for(var age in kids) {
        if( age >= min && age <= max) {
            age += kids[age];
        }
    }
    return age;
}


document.write( between(2,3) );
于 2012-07-10T13:35:35.957 回答