您的位置:首页 > 其它

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

2014-05-09 01:32 525 查看
一个穷举搜索题。

基本思路与穷举计算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
;
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优化可以加入排序,可以使得本质运算相同的结果不会重复计算。而且可以提高搜索的效率。

正常情况下这种性能已经可以接受
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: