void output(int array[], int no_of_operands, int answer) { int total_case=1; int count=1;
while (count<no_of_operands) { total_case=total_case*4; count++; } int u=0; while (u<total_case) { if (array==answer) { int r=u; while (r>=1) { if (r%4==0){cout<<"+";} else if (r%4==1){cout<<"-";} else if (r%4==2){cout<<"*";} else if (r%4==3){cout<<"/"<<endl;} r=r/4; } }
u++; } }
void finding_all_ans (int no_of_operands, int operands[],int answer) { int space[17000]; int temp[17000];
if(no_of_operands>1) // calculating all the possible with 2 operands { temp[0]=operands[0]+operands[1]; temp[1]=operands[0]-operands[1]; temp[2]=operands[0]*operands[1]; temp[3]=operands[0]/operands[1]; }
int count2=0; if(no_of_operands>2) // calculating all the possible with 3 operands { while (count2<4){
int count3=0; if(no_of_operands>3) // calculating all the possible with 4 operands { while (count3<16) { temp[4*count3]=space[count3]+operands[3]; temp[4*count3+1]=space[count3]-operands[3]; temp[4*count3+2]=space[count3]*operands[3]; temp[4*count3+3]=space[count3]/operands[3]; count3++; } }
int count4=0; if(no_of_operands>4) // calculating all the possible with 5 operands { while (count4<64) { space[4*count4]=temp[count4]+operands[4]; space[4*count4+1]=temp[count4]-operands[4]; space[4*count4+2]=temp[count4]*operands[4]; space[4*count4+3]=temp[count4]/operands[4]; count4++; } } int count5=0; if(no_of_operands>5) // calculating all the possible with 6 operands { while (count5<256) { temp[4*count5]=space[count5]+operands[5]; temp[4*count5+1]=space[count5]-operands[5]; temp[4*count5+2]=space[count5]*operands[5]; temp[4*count5+3]=space[count5]/operands[5]; count5++; } } int count6=0; if(no_of_operands>6) // calculating all the possible with 7 operands { while (count6<1024) { space[4*count6]=temp[count6]+operands[6]; space[4*count6+1]=temp[count6]-operands[6]; space[4*count6+2]=temp[count6]*operands[6]; space[4*count6+3]=temp[count6]/operands[6]; count6++; }
} int count7=0; if(no_of_operands>7) // calculating all the possible with 8 operands { while (count7<4096) { temp[4*count7]=space[count7]+operands[7]; temp[4*count7+1]=space[count7]-operands[7]; temp[4*count7+2]=space[count7]*operands[7]; temp[4*count7+3]=space[count7]/operands[7]; count7++; } }
if (no_of_operands==3||no_of_operands==5||no_of_operands==7) output(space,no_of_operands,answer); else output(temp,no_of_operands,answer); }
int main() { int no_of_operands; // inputing numbers of operands cin>>no_of_operands;
int operands[8]; // maximum size of no of operands = 8 int x=0; int no_of_solution; while (x<no_of_operands) { cin>>operands[x]; // inputing the operands x++; }