编译原理 三元式转四元式

三元式转四元式
程序给我 悬赏分没有问题

第1个回答  2013-07-29
#include<iostream>
#define LEN 9
using namespace std;
bool used[LEN];
int buf[LEN];
void output(int a[]){
/*
for(int i=0; i<LEN; i++)
cout<<a[i]<<" ";
cout<<endl;
*/
int x,y,z;
x=a[0]*10+a[1];
y=a[2]*100+a[3]*10+a[4];
z=a[5]*1000+a[6]*100+a[7]*10+a[8];
if(x*y==z)
cout<<x<<" * "<<y<<" = "<<z<<endl;
}
void dfs(int deep) {
if (deep == LEN) {
output(buf);
return;
}
for (int i = 0; i < LEN; i++){
if (used[i] == false) {
buf[deep] = i + 1;
used[i] = true;
dfs(deep+1);
used[i] = false;
}
}
}

int main() {
memset(used, false, sizeof(used));
dfs(0);
}
/*
还可以剪枝,懒得弄了,1秒钟应该可以出结果。
PS:没调试,是以前的代码改的,机器上没装编译器
*/
第2个回答  2013-07-29
http://bbs.51cto.com/thread-583470-1.html这个网页上有详细解答,参考下
相似回答