使用8个8进行任意拼接和四则运算,算出1000的计算步骤

一个穷举搜索题。

基本思路与穷举计算24点类似,24点是4个数,而这个是8个数。

如果一般化,可以添加一个运算“拼接”,且未参与任何四则运算的数可以进行该种运算。思路完全和算24是一样的。

代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

const int TARGET = 1000;
const int N = 8;
struct item
{
    string cal;
    int num;
    bool flag;
};
item a[N];
void swap(int i,int j)
{
    item t;t=a[i];a[i]=a[j];a[j]=t;
}
map<string,bool> mhash;
char buf[10];

bool push_map_check(int dep)
{
    string s = "";
    for(int i=0 ; i<dep ; i++)
    {
        itoa(a[i].num,buf,10);
        s = s + buf +',';
    }
    if(mhash[s] !=true )
    {
        mhash[s] = true;
        return true;
    }
    else
    {
        return false;
    }
}

void search_ans(int dep)
{
    if(dep == 1 && a[0].num==TARGET)
        cout << a[0].cal << endl;
    if(dep > 1 && push_map_check(dep))
    {
        //组合i,j
        map<int ,int> hash;
        hash.clear();
        for(int i=0 ; i<dep ; i++)
            for(int j=i+1 ; j<dep ; j++)
            {
                if(hash[a[i].num] == a[j].num) continue;
                if(hash[a[j].num] == a[i].num) continue;
                hash[a[i].num] = a[j].num;

                swap(j,dep-1);
                item temp = a[i];
                //+
                a[i].num = a[i].num + a[dep-1].num;
                a[i].flag = false;
                a[i].cal = '(' + temp.cal + '+' + a[dep-1].cal + ')';
                search_ans(dep-1);
                a[i] = temp;
                //-
                a[i].num = a[i].num - a[dep-1].num;
                a[i].flag = false;
                a[i].cal = '(' + temp.cal + '-' + a[dep-1].cal + ')';
                search_ans(dep-1);
                a[i] = temp;
                //*
                a[i].num = a[i].num * a[dep-1].num;
                a[i].flag = false;
                a[i].cal = '(' + temp.cal + '*' + a[dep-1].cal + ')';
                search_ans(dep-1);
                a[i] = temp;
                // /
                if(a[dep-1].num !=0 && a[i].num % a[dep-1].num == 0)
                {
                    a[i].num = a[i].num / a[dep-1].num;
                    a[i].flag = false;
                    a[i].cal = '(' + temp.cal + '/' + a[dep-1].cal + ')';
                    search_ans(dep-1);
                    a[i] = temp;
                }

                if(a[i].flag && a[dep-1].flag && a[dep-1].num == 8)
                {
                    a[i].num = a[i].num * 10 + a[dep-1].num;
                    a[i].cal = temp.cal + a[dep-1].cal;
                    search_ans(dep-1);
                    a[i] = temp;
                }
                swap(dep-1,j);
            }
    }
}


int main()
{
    //freopen("out.txt","w",stdout);
    mhash.clear();
    for(int i=0 ;i<N ; i++ )
    {
        a[i].cal = "8";
        a[i].num = 8;
        a[i].flag = true;
    }
    search_ans(N);
	return 0;

}

上面的程序为N个8计算得到1000的结果。可以修改参数得到不同的组合。

结果为


当然上面的hash优化可以加入排序,可以使得本质运算相同的结果不会重复计算。而且可以提高搜索的效率。

正常情况下这种性能已经可以接受

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值