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;

}

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;

}

</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} ${perm[3]})`;

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], arr[0]]] : 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>

相关推荐
屁股割了还要学8 小时前
【数据结构入门】排序算法(4)归并排序
c语言·数据结构·学习·算法·排序算法
再吃一根胡萝卜8 小时前
Git 强制推送指南:新手必读的「危险操作」解析
前端
努力学习的小廉8 小时前
我爱学算法之—— 位运算(上)
c++·算法
诚实可靠王大锤8 小时前
react-native项目通过华为OBS预签名url实现前端直传
前端·react native·华为
Monly218 小时前
Vue:下拉框多选影响行高
前端·javascript·vue.js
是罐装可乐8 小时前
前端架构知识体系:Source Map 的用法和全面解析
前端·映射·前端架构·代码安全·源码地图·source-map
ゞ 正在缓冲99%…8 小时前
leetcode35.搜索插入位置
java·算法·leetcode·二分查找
小桥风满袖8 小时前
极简三分钟ES6 - ES8中对象扩展
前端·javascript
lifallen8 小时前
字节跳动Redis变种Abase:无主多写架构如何解决高可用难题
数据结构·redis·分布式·算法·缓存
代码的余温8 小时前
Web服务器VS应用服务器:核心差异解析
运维·服务器·前端