But the judge give me "Compile Error".
Can anyone help me?
Code: Select all
#include <stack>
#include <vector>
#include <iostream>
using namespace std;
stack<int> o;
stack<int> d;
vector<int> com;
int x[11]={0};
int y[11]={0};
int t, n;
bool success;
bool ADD();
bool SUB();
bool DIV();
bool MUL();
bool DUP();
void ID(int);
void initialize()
{
while(!o.empty()) o.pop();
while(!d.empty()) d.pop();
success = 0;
}
int main(void)
{
int i, c=1;
while(cin >> n && n != 0) {
initialize();
com.clear();
for(i = 0; i < n; i++) cin >> x[i];
for(i = 0; i < n; i++) cin >> y[i];
cout<<"Program "<<c++<<endl;
o.push(x[n-1]);
d.push(y[n-1]);
for(int i = 1; i <= 10; i++) {
ID(i);
if(success) {
if(com.size() == 0)
cout<<"Empty sequence";
else
for( i = 0; i <= com.size()-1; i++) {
switch(com.at(i)) {
case 1:
cout<<"ADD";
break;
case 2:
cout<<"SUB";
break;
case 3:
cout<<"MUL";
break;
case 4:
cout<<"DIV";
break;
case 5:
cout<<"DUP";
break;
}
if(i != com.size()-1) cout<<" ";
}
cout<<endl;
break;
}
}
if(success == 0)
cout<<"Impossible"<<endl;
cout<<endl;
}
return 0;
}
void ID(int c) //iterating deepening
{
if(o.top() == d.top() && o.size() == 1 && d.size() == 1) {
int tx = o.top(), ty = d.top();
for(int i = 0; i < n-1; i++) {
initialize();
o.push(x[i]);
d.push(y[i]);
for(int j = 0; j <= com.size()-1; j++) {
switch(com.at(j)) {
case 1:
ADD();
break;
case 2:
SUB();
break;
case 3:
MUL();
break;
case 4:
DIV();
break;
case 5:
DUP();
break;
}
}
if(o.size() != 1 || o.top() != d.top()) {
initialize();
o.push(tx);
d.push(ty);
return;
}
}
success = 1;
return ;
}
else if(c == 0) {
return ;
}
else {
stack<int> tmp(o);
com.push_back(1);
if(ADD()) ID(c-1);
if(success) return ;
com.pop_back();
o = tmp;
com.push_back(4);
if(DIV()) ID(c-1);
if(success) return ;
com.pop_back();
o = tmp;
com.push_back(5);
if(DUP()) ID(c-1);
if(success) return ;
com.pop_back();
o = tmp;
com.push_back(3);
if(MUL()) ID(c-1);
if(success) return ;
com.pop_back();
o = tmp;
com.push_back(2);
if(SUB()) ID(c-1);
if(success) return ;
com.pop_back();
o = tmp;
}
}
bool ADD()
{
if(o.size() > 1) {
t = o.top(); o.pop();
t += o.top(); o.pop();
if(t > 30000) return 0;
o.push(t);
return 1;
}
return 0;
}
bool SUB()
{
if(o.size() > 1) {
t = -1 * o.top(); o.pop();
t += o.top(); o.pop();
if(t > 30000) return 0;
o.push(t);
return 1;
}
return 0;
}
bool MUL()
{
if(o.size() > 1) {
t = o.top(); o.pop();
t *= o.top(); o.pop();
if(t > 30000) return 0;
o.push(t);
return 1;
}
return 0;
}
bool DIV()
{
if(o.size() > 1 && o.top() != 0) {
t = o.top(); o.pop();
t = o.top() / t; o.pop();
if(t > 30000) return 0;
o.push(t);
return 1;
}
return 0;
}
bool DUP()
{
t = o.top();
o.push(t);
return 1;
}