function createQueue(onMatch) {
const queue = [];
const checkItem = (item, range, i) => {
if (item[0] <= range[0] && range[1] <= item[1]) {
queue.splice(i, 1);
onMatch(range, item);
return true;
}
return false;
};
const add = (range) => {
for (let i = 0; i < queue.length; ++i) {
if (checkItem(queue[i], range, i) || checkItem(range, queue[i], i)) {
return;
}
}
queue.push(range);
};
return {queue, add};
}
// пример использования
const q = createQueue((a, b) => console.log(a, b));
q.add([2, 4]);
q.add([3, 5]);
q.add([4, 4]); // log ([4, 4], [2, 4])
#include <iostream>
struct Node {
int value;
Node *next;
};
void RecursiveOutput( Node* item )
{
if ( item != 0 )
{
RecursiveOutput(item->next);
std::cout << item->value << std::endl;
}
}
void RecursiveInput( int n, int index = 1, Node* item = 0 )
{
if ( index <= n )
{
Node data;
std::cin >> data.value;
data.next = item;
RecursiveInput( n, index + 1, &data );
} else {
std::cout << "RecursiveOutput" << std::endl;
RecursiveOutput(item);
}
}
int DEPTH = 3;
int main()
{
std::cout << "input values: " << std::endl;
RecursiveInput(DEPTH);
std::cout << "end" << std::endl;
return 0;
}
function floorCount(start, max) {
let result = 0;
let prevStart = -1;
while (start > 0) {
for (let x = start; x <= max && x !== prevStart; x = x % 2 ? 2 * x : 2 * x - 1) {
result++;
}
prevStart = start;
start = start < 2 ? 0 : start % 2 ? (start + 1) / 2 : start / 2;
}
return result;
}
function bankomat(sum, coins, maxCC) {
const full = coins.reduce((sum, c) => sum + c * maxCC, 0);
if (full < sum) return -1;
const dp = Array.from({length: sum + 1}, () => ({
count: full + 1,
coins: {},
}));
coins.sort((a, b) => a - b);
dp[0].count = 0;
for (let i = 1; i <= sum; ++i) {
const item = dp[i];
let optCoin = 0;
for (let j = 0; j < coins.length; ++j) {
const coin = coins[j];
if (coin > i) break;
const prev = dp[i - coin];
if (item.count > prev.count + 1 && (prev.coins[coin] || 0) < maxCC) {
item.count = prev.count + 1;
optCoin = coin;
}
}
if (optCoin) {
const prev = dp[i - optCoin];
item.coins = {
...prev.coins,
[optCoin]: (prev.coins[optCoin] || 0) + 1
};
}
}
if (dp[sum].count > full) {
return 0;
}
const arr = [];
Object.keys(dp[sum].coins).forEach((coin) => {
const c = dp[sum].coins[coin];
for (let i = 0; i < c; ++i) {
arr.push(Number(coin));
}
});
return arr;
}
console.log(bankomat(100, [11, 20, 30, 40, 12, 99], 2)); // [20, 40, 40]
function run() {
const arr = Array.from({length: 5}, () => Array(5).fill('0'));
const pos = [0, 0];
while(pos[0] !== 4 || pos[1] !== 4) {
if (pos[0] === 4) {
pos[1]++;
} else if (pos[1] === 4) {
pos[0]++;
} else {
const r = Math.floor(Math.random() * 3) + 1;
if (r & 1) {
pos[0]++;
}
if (r & 2) {
pos[1]++;
}
}
arr[pos[0]][pos[1]] = 'X';
}
arr[0][0] = 'S';
arr[4][4] = 'E';
return arr;
}
run()