来自:https://ip138.com/24dian/
(function(){ function calc24(){ var expression = [].slice.call(arguments).sort(); var operator = ['+','-','*','/'],result = [],hash = {}; (function(expression){ var len = expression.length; var group_str = expression.slice(0).sort().toString(); if(!hash[group_str]){ hash[group_str] = true; if(len>1){ for(var i=0;i<len-1;i++){ for(var j=i+1;j<len;j++){ var sort_expression = expression.slice(0); var exp1 = sort_expression.splice(j,1)[0]; var exp2 = sort_expression.splice(i,1)[0]; for(var n=0;n<4;n++){ var new_expression = sort_expression.slice(0); new_expression.splice(0,0,n>1||len==2?exp1+operator[n]+exp2:'('+exp1+operator[n]+exp2+')'); arguments.callee(new_expression); if(exp1!=exp2&&n%2){ new_expression.splice(0,1,n>1||len==2?exp2+operator[n]+exp1:'('+exp2+operator[n]+exp1+')'); arguments.callee(new_expression); } } } } }else if(Math.abs(eval(expression[0])-24)<1e-6){ result.push(expression[0]); } } })(expression); return result; } var $inputs = DOMUtil.getElementsByClassName('input-text'); DOMUtil.getElementsByClassName('input-button')[0].onclick = function(){ var num1 = +($inputs[0].value)|| 0; var num2 = +($inputs[1].value)|| 0; var num3 = +($inputs[2].value)|| 0; var num4 = +($inputs[3].value)|| 0; var arr = calc24(num1,num2,num3,num4); var str = arr.length?'':'不能够计算得到!'; for(var i = arr.length;i--;){ str += arr[i]+'\n'; } document.getElementsByTagName('TEXTAREA')[0].value = str; } })();
标签:24,exp2,exp1,len,var,计算器,operator,expression From: https://www.cnblogs.com/z5337/p/17027394.html