function combine(array, sum) {
function c(left, right, sum) {
if (!sum) {
result = right;
return true;
}
return left.some(function (a, i, aa) {
return a <= sum && c(aa.slice(i + (a > sum - a)), right.concat(a), sum - a);
});
}
var result = [];
c(array.sort(function (a, b) { return b - a; }), [], sum);
return result;
}
var lottery = [{ name: 'twig', price: 5, win: 40 }, { name: 'rock', price: 10, win: 80 }, { name: 'shell', price: 28, win: 250 }, { name: 'chip', price: 50, win: 400 }, { name: 'gold', price: 56, win: 500 }, { name: 'diamond', price: 280, win: 2500 }],
lotteryByPrice = lottery.reduce(function (r, a) { r[a.price] = a; return r; }, Object.create(null)),
getItems = function() {
var price = 0,
array = lottery.map(function (a) { return a.price; });
return function() {
var temp;
do {
temp = combine(array, price);
price++;
} while (!temp.length)
return temp;
}
}(),
createTableRow = function (element) {
var table = document.createElement('table'),
tr = document.createElement('tr');
['Game', 'Items', 'Types', 'Spend Per Game', 'Total Spend', 'Max. Possible Winnigs', 'Investment'].forEach(function (a) {
var th = document.createElement('th');
th.appendChild(document.createTextNode(a));
tr.appendChild(th);
});
table.appendChild(tr);
element.appendChild(table);
return function (row) {
var tr = document.createElement('tr');
['game', 'items', 'types', 'spend', 'total', 'potential', 'investment'].forEach(function (k) {
var td = document.createElement('td');
td.appendChild(document.createTextNode(row[k]));
tr.appendChild(td);
});
if (row.topBorder) {
tr.style.borderTop = '2px solid #666';
}
table.appendChild(tr);
};
}(document.body),
row = { game: null, items: null, types: null, spend: null, total: 0, potential: null, investment: null },
i,
items = getItems(),
add = function (a, b) { return a + b; },
winP = function (a) { return lotteryByPrice[a].win; },
nameP = function (a) { return lotteryByPrice[a].name; };
for (i = 1; i <= 70; i++) {
row.topBorder = false;
while (row.total - items.reduce(add) + items.map(winP).reduce(add) < 0) {
items = getItems();
row.topBorder = true;
}
row.game = i;
row.items = items.length;
row.types = items.map(nameP).join(' + ');
row.spend = -items.reduce(add);
row.total += row.spend;
row.potential = items.map(winP).reduce(add);
row.investment = row.potential + row.total;
createTableRow(row);
}
table { border-collapse: collapse; font-family: Sans-Serif; }
th { border: 1px solid #ccc; padding: 0 10px; }
td { text-align: center; border: 1px solid #ccc; }
ですか – guest271314
@ guest271314 No. –