Bipins Dot Net daily bits and pieces

14Feb/102

Given digits and values, find suitable expression

/* 

 Write a function that given a string of digits and a target value,
 prints where to put +'s and *'s between the digits so
 they combine exactly to the target value.
 Note there may be more than one answer,
 it doesn't matter which one you print.

Examples:
"1231231234",11353 -> "12*3+1+23*123*4"
"3456237490",1185 -> "3*4*56+2+3*7+490"
"3456237490",9191 -> "no solution"

 program made in windows / cygwin g++  with Netbeans IDE

*/

#include <iostream>
#include <string>
#include <math.h>
#include <vector>

using namespace std;

string findExprSymbols(int n, int pos)
{
    string s = "+*-";

    int rem = pos;
    int div = 1;
    string ret = "";
    for(int j=0;j<n;j++)
        {

            div = (int)pow(3,n-(j+1));
            int index = rem / div ;
            rem = rem  % div;
            ret += s.at(index);
        }
   return ret;
}

string getExpres(string digits,string expression,int & value)
{
    string exp="";
    exp= exp + digits.at(0);
    string current = exp;
    vector<string> mystack;
    int last = 0;
    for(int i=0;i<expression.size();i++)
    {
        switch(expression.at(i))
        {
            case '+':
                exp= exp + '+'+digits.at(i+1);
                mystack.push_back(current);
                mystack.push_back("+");
                current= digits.at(i+1);
                break;
            case '-':
                exp= exp + digits.at(i+1);
                current = current + digits.at(i+1);
                break;
            case '*':
                exp= exp + '*'+digits.at(i+1);
                mystack.push_back(current);
                mystack.push_back("*");
                current= digits.at(i+1);
                break;
        }
    }
    mystack.push_back(current);
    vector<int> evalstack;
    int j=mystack.size()-1;
    int temp;
    for(;j>=0;j--)
    {
        string str = mystack.at(j);
        if(str=="*")
        {
            temp = evalstack.back();
            temp = atoi(mystack.at(j-1).c_str()) * temp;
            evalstack.pop_back();
            evalstack.push_back(temp);
            j--;
        }
        else if( str == "+")
        {
            temp = atoi(mystack.at(j-1).c_str());
            evalstack.push_back(temp);
            j--;
        }
        else{
            temp = atoi(str.c_str());
            evalstack.push_back(temp);
        }
    }
    int final = 0;
    for(int x=0;x<evalstack.size();x++)
    {
        final+=evalstack.at(x);
    }
    value = final;
    //cout<<final<<endl;
    return exp;
}

int main(int argc, char** argv) {
    string digits ;
    int result;
    cin>>digits;
    cin>>result;

    //string digits="1231231234";
    int value;
    double total = pow(3,digits.size()-1);
    bool done=false;
    for(int i=0;i<total;i++)
    {
        string expr = findExprSymbols(digits.size()-1,i);
        string myexp = getExpres(digits,expr,value);

            if( value == result)
            {
                cout<<myexp<<"="<<value<<endl;
                done = true;
                break;
            }
    }
    if(!done)
    {
        cout<<" no solution"<<endl;
    }
    return (EXIT_SUCCESS);
}

Filed under: old Leave a comment
Comments (2) Trackbacks (0)
  1. Thanks Binit.

    If you remember, this one uses non recursive approach by using a for loop till a limit calculated by its possible number of execution and a replacement of recursion by a formula. :)


Leave a comment


No trackbacks yet.

6 visitors online now
6 guests, 0 members
All time: 15 at 07-27-2010 07:36 am UTC
Max visitors today: 6 at 12:58 pm UTC
This month: 6 at 09-07-2010 12:58 pm UTC
This year: 15 at 07-27-2010 07:36 am UTC