html+JavaScript+css 24点计算器

源代码 采用穷举计算方法

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

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>24 Point Game</title>

<style>

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;

}

inputtype="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;

}

</style>

</head>

<body>

<div class="game-container">

<h1>24 Point Game</h1>

<input type="number" id="num1" placeholder="Enter Number 1">

<input type="number" id="num2" placeholder="Enter Number 2">

<input type="number" id="num3" placeholder="Enter Number 3">

<input type="number" id="num4" placeholder="Enter Number 4">

<button οnclick="solve24Point()">Calculate</button>

<div class="results" id="results"></div>

</div>

<script>

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} ${perm3})`;

let result = evalExpression(expr);

if (Math.abs(result - 24) < 0.01) {

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

}

}

}

}

}

return solutions;

}

function getPermutations(arr) {

if (arr.length <= 2) return arr.length === 2 ? arr, \[arr\[1, arr0]] : arr;

return arr.reduce((acc, item, i) => 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 += `<p>${solution}</p>`;

});

} else {

resultsDiv.innerHTML = '<p>No solution found.</p>';

}

}

</script>

</body>

</html>

相关推荐
默_笙1 分钟前
🍕 一个主编、三个工种、两本手册:搭一支 AI 调研队
前端·javascript
纸片人4 分钟前
Blender 建模 + Three.js 展示:和 AI 一起做一个光储充超充站数字孪生大屏
前端
智能直播5 分钟前
网络RTMP拉流不卡顿、声音不同步?一文讲透缓冲区、时间戳与MEDAI V2实战调优
前端
第七页独白7 分钟前
汽车零件厂如何通过 QMS 真正落地 IATF 16949——QMS软件系统:品质检验-内审稽核-8d客诉管理:全星质量管理软件系统
java·前端·数据库
纸片人14 分钟前
只用 three.js + OpenStreetMap,手搓一个「成都城市 3D」数据大屏
前端
星若生辉15 分钟前
Hugo 静态站点搭建|从本地调试到上线部署实操指南
前端
沐浴露z32 分钟前
Agent 响应延迟过高?从工具治理角度提供两条思路
前端·网络
CappuccinoRose44 分钟前
FormData数据处理
开发语言·前端·javascript·表单数据
飘尘2 小时前
SVG和Canvas,前端里的两支“画笔”,用的时候怎么选择?
前端·javascript·面试
飘尘2 小时前
从"算子"到"AI Infra":大模型背后看不见的那群人在忙什么
人工智能·算法·面试