html+JavaScript+css 24点计算器

07-07 1084阅读

源代码       采用穷举计算方法

html+JavaScript+css 24点计算器
(图片来源网络,侵删)

讲人话:根据四个数随机列算式,算出来是24就显示在列表里。

24 Point Game

    body {

        background: #f0f0f0;

        font-family: Arial, sans-serif;

        margin: 0;

        padding: 0;

        display: flex;

        justify-content: center;

        align-items: center;

        min-height: 100vh;

    }

    .game-container {

        background: #fff;

        border-radius: 10px;

        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);

        padding: 20px;

        width: 400px;

    }

    h1 {

        text-align: center;

        color: #333;

    }

    input[type="number"] {

        width: 100%;

        padding: 10px;

        margin-bottom: 10px;

        border: none;

        border-radius: 5px;

        box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);

    }

    button {

        width: 100%;

        padding: 10px;

        background: #007bff;

        color: #fff;

        border: none;

        border-radius: 5px;

        cursor: pointer;

        transition: background 0.3s;

    }

    button:hover {

        background: #0056b3;

    }

    .results {

        margin-top: 20px;

        color: #333;

    }

    .results p {

        margin: 5px 0;

    }

   

24 Point Game

   

   

   

   

    Calculate

   

function solve24Point() {

    const numbers = [

        parseFloat(document.getElementById('num1').value),

        parseFloat(document.getElementById('num2').value),

        parseFloat(document.getElementById('num3').value),

        parseFloat(document.getElementById('num4').value)

    ];

    const solutions = findSolutions(numbers);

    displaySolutions(solutions);

}

function findSolutions(nums) {

    const ops = ['+', '-', '*', '/'];

    const solutions = [];

    const permutations = getPermutations(nums);

    

    for (let perm of permutations) {

        for (let op1 of ops) {

            for (let op2 of ops) {

                for (let op3 of ops) {

                    let expr = `(${perm[0]} ${op1} ${perm[1]}) ${op2} (${perm[2]} ${op3} ${perm[3]})`;

                    let result = evalExpression(expr);

                    if (Math.abs(result - 24)

                        solutions.push(`(${perm[0]} ${op1} ${perm[1]}) ${op2} (${perm[2]} ${op3} ${perm[3]}) = ${result.toFixed(2)}`);

                    }

                }

            }

        }

    }

    return solutions;

}

function getPermutations(arr) {

    if (arr.length acc.concat(

        getPermutations(arr.slice(0, i).concat(arr.slice(i + 1)))

            .map(x => [item].concat(x))

    ), []);

}

function evalExpression(expression) {

    try {

        return eval(expression);

    } catch (e) {

        return NaN;

    }

}

function displaySolutions(solutions) {

    const resultsDiv = document.getElementById('results');

    resultsDiv.innerHTML = '';

    if (solutions.length > 0) {

        solutions.forEach(solution => {

            resultsDiv.innerHTML += `

${solution}

`;

        });

    } else {

        resultsDiv.innerHTML = '

No solution found.

';

    }

}

 

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]