24点游戏,输出所有解,数字可以任意个数
function equal(o1,o2){
if(o1===o2){return true;}
if(o1!==o1){
return o2!==o2;
}
if(typeof(o1)!==typeof(o2)){
return false;
}
if(String(o1)!==String(o2)){
return false;
}
if(typeof(o1)===typeof([])){
// if(o1.length!==o2.length){
// return false;
// }
// b1=true
// for(i in o1){
// if(!equal(o1[i],o2[i])){
// return false
// }
// }
if(JSON.stringify(o1)[0]!==JSON.stringify(o2)[0]){
return false;
}
if(Object.keys(o1).length===Object.keys(o2).length){
b2=false
for(let i in o1){
if(!equal(o1[i],o2[i])){
return false
}
}
return true
}else{
return false;
}
return true
}
if(typeof(o1)===typeof({})){
}
}
Array.prototype.remove = function(val) {
let i=0,chongfu_index=-1
for(let one of this){
if(equal(one,val)){
chongfu_index=i
}
i++
}
var index = chongfu_index;
if (index > -1) {
this.splice(index, 1);
}
};
ii=0
ls=[]
target_num=24
ls1=[]
i=0
var judgePoint24 = function (nums,ii) {
if(ls.length===11){
sss=1
}
if(i==='remove'){
sss=1
}
nums_copy=nums.slice()
nums_copy.sort()
for(num_ls of ls){
num_ls.sort()
}
b=false
for(num_ls of ls){
if(equal(nums_copy,num_ls)){
b=true
}
}
if(b){
return false;
}else{
ls.push(nums_copy.slice())
}
if (nums.length == 1){
if( Math.abs(nums[0] - target_num) < 1e-6){
ls2=[]
for(let k in ls1)
{
ls2.push(ls1[k])
}
console.log(ls2)
ls2=[]
}
}
for (let i = 0; i < nums.length; i++){
for (let j = i+1; j < nums.length; j++) {
if(ls.length>9){
sss=1
}
if(i==='remove'){
sss=1
}
if(i===j){
continue
}
let rest = nums.filter((value, index) => index != i && index != j);
if(ii!==i){
ls1.push("~"+nums[i]+'='+~nums[i])
judgePoint24([~nums[i],...[nums[j]].concat(rest)],0)
ls1.remove("~"+nums[i]+'='+~nums[i])
}
else{
b=false
}
ls1.push(nums[i]+'+'+nums[j]+'='+(nums[i] + nums[j]))
judgePoint24([nums[i] + nums[j], ...rest])
ls1.remove(nums[i]+'+'+nums[j]+'='+(nums[i] + nums[j]))
ls1.push(nums[i]+'*'+nums[j]+'='+(nums[i] * nums[j]))
judgePoint24([nums[i] * nums[j], ...rest])
ls1.remove(nums[i]+'*'+nums[j]+'='+(nums[i] * nums[j]))
ls1.push(nums[i]+'-'+nums[j]+'='+(nums[i] - nums[j]))
judgePoint24([nums[i] - nums[j], ...rest])
ls1.remove(nums[i]+'-'+nums[j]+'='+(nums[i] - nums[j]))
ls1.push(nums[j]+'-'+nums[i]+'='+(nums[j] - nums[i]))
judgePoint24([nums[j] - nums[i], ...rest])
ls1.remove(nums[j]+'-'+nums[i]+'='+(nums[j] - nums[i]))
ls1.push(nums[i]+'/'+nums[j]+'='+(nums[i] / nums[j]))
judgePoint24([nums[i] / nums[j], ...rest])
ls1.remove(nums[i]+'/'+nums[j]+'='+(nums[i] / nums[j]))
ls1.push(nums[j]+'/'+nums[i]+'='+(nums[j] / nums[i]))
judgePoint24([nums[j] / nums[i], ...rest])
ls1.remove(nums[j]+'/'+nums[i]+'='+(nums[j] / nums[i]))
ls1.push(nums[i]+'%'+nums[j]+'='+(nums[i] % nums[j]))
judgePoint24([nums[i] % nums[j], ...rest])
ls1.remove(nums[i]+'%'+nums[j]+'='+(nums[i] % nums[j]))
ls1.push(nums[j]+'%'+nums[i]+'='+(nums[j] % nums[i]))
judgePoint24([nums[j] % nums[i], ...rest])
ls1.remove(nums[j]+'%'+nums[i]+'='+(nums[j] % nums[i]));
ls1.push(nums[i]+'**'+nums[j]+'='+(nums[i] ** nums[j]))
judgePoint24([nums[i] ** nums[j], ...rest])
ls1.remove(nums[i]+'**'+nums[j]+'='+(nums[i] ** nums[j]));
ls1.push(nums[j]+'**'+nums[i]+'='+(nums[j] ** nums[i]))
judgePoint24([nums[j] ** nums[i], ...rest])
ls1.remove(nums[j]+'**'+nums[i]+'='+(nums[j] ** nums[i]));
ls1.push(nums[i]+'&'+nums[j]+'='+(nums[i] & nums[j]))
judgePoint24([nums[i] & nums[j], ...rest])
ls1.remove(nums[i]+'&'+nums[j]+'='+(nums[i] & nums[j]))
ls1.push(nums[i]+'|'+nums[j]+'='+(nums[i] | nums[j]))
judgePoint24([nums[i] | nums[j], ...rest])
ls1.remove(nums[i]+'|'+j+'='+(nums[i] | nums[j]))
ls1.push(nums[i]+'^'+nums[j]+'='+(nums[i] ^ nums[j]))
judgePoint24([nums[i] ^ nums[j], ...rest])
ls1.remove(nums[i]+'^'+nums[j]+'='+(nums[i] ^ nums[j]))
ls1.push(nums[i]+">>"+nums[j]+'='+(nums[i] >> nums[j]))
judgePoint24([nums[i] >> nums[j], ...rest])
ls1.remove(nums[i]+">>"+nums[j]+'='+(nums[i] >> nums[j]));
ls1.push(nums[i]+">>"+nums[j]+'='+(nums[j] >> nums[i]))
judgePoint24([nums[j] >> nums[i], ...rest])
ls1.remove(nums[i]+">>"+nums[j]+'='+(nums[j] >> nums[i]));
ls1.push(nums[i]+"<<"+nums[j]+'='+(nums[i] << nums[j]))
judgePoint24([nums[i] << nums[j], ...rest])
ls1.remove(nums[i]+"<<"+nums[j]+'='+(nums[i] << nums[j]));
ls1.push(nums[j]+"<<"+nums[i]+'='+(nums[j] << nums[i]))
judgePoint24([nums[j] << nums[i], ...rest])
ls1.remove(nums[j]+"<<"+nums[i]+'='+(nums[j] << nums[i]));
}
}
return false
}
for(let i=1;i<=9;i++){
console.log(`5个${i}:`)
let r=judgePoint24([i,i,i,i,i])
ls=[]
ls2=[]
console.log(r)
}
s=''
function equal(o1,o2){
if(o1===o2){return true;}
if(o1!==o1){
return o2!==o2;
}
if(typeof(o1)!==typeof(o2)){
return false;
}
if(String(o1)!==String(o2)){
return false;
}
if(typeof(o1)===typeof([])){
// if(o1.length!==o2.length){
// return false;
// }
// b1=true
// for(i in o1){
// if(!equal(o1[i],o2[i])){
// return false
// }
// }
if(JSON.stringify(o1)[0]!==JSON.stringify(o2)[0]){
return false;
}
if(Object.keys(o1).length===Object.keys(o2).length){
b2=false
for(let i in o1){
if(!equal(o1[i],o2[i])){
return false
}
}
return true
}else{
return false;
}
return true
}
if(typeof(o1)===typeof({})){
}
}
Array.prototype.remove = function(val) {
let i=0,chongfu_index=-1
for(let one of this){
if(equal(one,val)){
chongfu_index=i
}
i++
}
var index = chongfu_index;
if (index > -1) {
this.splice(index, 1);
}
};
ii=0
ls=[]
target_num=24
ls1=[]
i=0
var judgePoint24 = function (nums,ii) {
if(ls.length===11){
sss=1
}
if(i==='remove'){
sss=1
}
nums_copy=nums.slice()
nums_copy.sort()
for(num_ls of ls){
num_ls.sort()
}
b=false
for(num_ls of ls){
if(equal(nums_copy,num_ls)){
b=true
}
}
if(b){
return false;
}else{
ls.push(nums_copy.slice())
}
if (nums.length == 1){
if( Math.abs(nums[0] - target_num) < 1e-6){
ls2=[]
for(let k in ls1)
{
ls2.push(ls1[k])
}
console.log(ls2)
ls2=[]
}
if( Math.abs(~nums[0] - target_num) < 1e-6){
ls2=[]
ls1.push(`~${nums[0]}=${~nums[0]}`)
for(let k in ls1)
{
ls2.push(ls1[k])
}
console.log(ls2)
ls1.remove(`~${nums[0]}=${~nums[0]}`)
ls2=[]
}
}
for (let i = 0; i < nums.length; i++){
for (let j = i+1; j < nums.length; j++) {
if(i===1&&j===2){
sss=1
}
if(ls.length>9){
sss=1
}
if(i==='remove'){
sss=1
}
if(i===j){
continue
}
let rest = nums.filter((value, index) => index != i && index != j);
ls1.push(nums[i]+'+'+nums[j]+'='+(nums[i] + nums[j]))
judgePoint24([nums[i] + nums[j], ...rest])
ls1.remove(nums[i]+'+'+nums[j]+'='+(nums[i] + nums[j]))
ls1.push(nums[i]+'*'+nums[j]+'='+(nums[i] * nums[j]))
judgePoint24([nums[i] * nums[j], ...rest])
ls1.remove(nums[i]+'*'+nums[j]+'='+(nums[i] * nums[j]))
ls1.push(nums[i]+'-'+nums[j]+'='+(nums[i] - nums[j]))
judgePoint24([nums[i] - nums[j], ...rest])
ls1.remove(nums[i]+'-'+nums[j]+'='+(nums[i] - nums[j]))
ls1.push(nums[j]+'-'+nums[i]+'='+(nums[j] - nums[i]))
judgePoint24([nums[j] - nums[i], ...rest])
ls1.remove(nums[j]+'-'+nums[i]+'='+(nums[j] - nums[i]))
ls1.push(nums[i]+'/'+nums[j]+'='+(nums[i] / nums[j]))
judgePoint24([nums[i] / nums[j], ...rest])
ls1.remove(nums[i]+'/'+nums[j]+'='+(nums[i] / nums[j]))
ls1.push(nums[j]+'/'+nums[i]+'='+(nums[j] / nums[i]))
judgePoint24([nums[j] / nums[i], ...rest])
ls1.remove(nums[j]+'/'+nums[i]+'='+(nums[j] / nums[i]))
ls1.push(nums[i]+'%'+nums[j]+'='+(nums[i] % nums[j]))
judgePoint24([nums[i] % nums[j], ...rest])
ls1.remove(nums[i]+'%'+nums[j]+'='+(nums[i] % nums[j]))
ls1.push(nums[j]+'%'+nums[i]+'='+(nums[j] % nums[i]))
judgePoint24([nums[j] % nums[i], ...rest])
ls1.remove(nums[j]+'%'+nums[i]+'='+(nums[j] % nums[i]));
ls1.push(nums[i]+'**'+nums[j]+'='+(nums[i] ** nums[j]))
judgePoint24([nums[i] ** nums[j], ...rest])
ls1.remove(nums[i]+'**'+nums[j]+'='+(nums[i] ** nums[j]));
ls1.push(nums[j]+'**'+nums[i]+'='+(nums[j] ** nums[i]))
judgePoint24([nums[j] ** nums[i], ...rest])
ls1.remove(nums[j]+'**'+nums[i]+'='+(nums[j] ** nums[i]));
ls1.push(nums[i]+'&'+nums[j]+'='+(nums[i] & nums[j]))
judgePoint24([nums[i] & nums[j], ...rest])
ls1.remove(nums[i]+'&'+nums[j]+'='+(nums[i] & nums[j]))
ls1.push(nums[i]+'|'+j+'='+(nums[i] | nums[j]))
judgePoint24([nums[i] | nums[j], ...rest])
ls1.remove(nums[i]+'|'+j+'='+(nums[i] | nums[j]))
ls1.push(nums[i]+'^'+nums[j]+'='+(nums[i] ^ nums[j]))
judgePoint24([nums[i] ^ nums[j], ...rest])
ls1.remove(nums[i]+'^'+nums[j]+'='+(nums[i] ^ nums[j]))
ls1.push(nums[i]+">>"+nums[j]+'='+(nums[i] >> nums[j]))
judgePoint24([nums[i] >> nums[j], ...rest])
ls1.remove(nums[i]+">>"+nums[j]+'='+(nums[i] >> nums[j]));
ls1.push(nums[i]+">>"+nums[j]+'='+(nums[j] >> nums[i]))
judgePoint24([nums[j] >> nums[i], ...rest])
ls1.remove(nums[i]+">>"+nums[j]+'='+(nums[j] >> nums[i]));
ls1.push(nums[i]+"<<"+nums[j]+'='+(nums[i] << nums[j]))
judgePoint24([nums[i] << nums[j], ...rest])
ls1.remove(nums[i]+"<<"+nums[j]+'='+(nums[i] << nums[j]));
ls1.push(nums[j]+"<<"+nums[i]+'='+(nums[j] << nums[i]))
judgePoint24([nums[j] << nums[i], ...rest])
ls1.remove(nums[j]+"<<"+nums[i]+'='+(nums[j] << nums[i]));
if(ii!==i){
ls1.push("~"+nums[i]+'='+~nums[i])
judgePoint24([...[nums[j]].concat(rest),~nums[i]])
ls1.remove("~"+nums[i]+'='+~nums[i])
}
else{
b=false
}
}
}
return false
}
for(let i=1;i<=9;i++){
console.log(`5个${i}:`)
let r=judgePoint24([i,i,i,i,i])
ls=[]
ls2=[]
console.log(r)
}
judgePoint24([-1000,-1000,-1000,-1000,-1000])