Problem

Started by
5 comments, last by alvaro 15 years, 5 months ago
Hi, I ran into a problem while tring to solve this programming question for a test a while back. I needed to write a function that takes in a string of numbers and a value. the function should write out to the screen the combanations of +'s and *'s of the string of numbers to get the vaule passed in. All the numbers in the string have to be used. I can't figure out how to write this. If anyone can help me that would be great.
Advertisement
What language are you writing this in? What code have you so far?

You should know that directly answering such homework questions is against the forum rules. At most, you will get tips as to what is wrong in your current implementation, and hints as to what to try next.
Well i didn't get anywhere i didn't even know where to start. and for homework this wasn't this was a question from a entry level programming test from a game development company. I didn't get this question and would love to know how you actually do this.
The thing is though, we don't have any way to tell your question apart from random homework assignment #42. So we just assume that "homework-like" questions are homework.

It doesn't even matter whether it is homework. If you seriously don't even know where to start, then giving you the code will not help you in any way.

If you want somewhere to start, try writing a similar function that takes a string of numbers and returns the sum of these numbers. Once you have this out of the way, you are maybe a third of the way there.
If they were stupid enough to not state that you can only use the numbers in the string then the answer is simple:

Add all the numbers in the string, multiply by 0 and add the final value. Done ;)
-Skiller
I wish it was that simple. The example they gave me that i needed to reproduce for the test they showedthe actual combanation of the numbers that needed to be printed to the screen
This is how I would do it (in C++):
#include <iostream>#include <string>#include <sstream>template <typename T>std::string to_string(T const &t) {  std::ostringstream oss;  oss << t;  return oss.str();}struct IntWithFormula {  int value;  std::string formula;  bool formula_is_sum;    IntWithFormula(int n) :    value(n),    formula(to_string(n)),    formula_is_sum(false) {  }};template <typename X>struct Backup : X {  X &ref;    Backup(X &ref) : X(ref), ref(ref) {  }  ~Backup() {    ref=*this;  }};bool show_me_how_it_is_done(IntWithFormula *ingredients, int n, int value) {  if(n==1) {    if(ingredients[0].value==value) {      std::cout << ingredients[0].formula << " = " << value << "\n";      return true;    }    return false;  }    for(int i=0;i<n;++i) {    for(int j=i+1;j<n;++j) {      Backup<IntWithFormula> save_i(ingredients), save_j(ingredients[j]);      ingredients.value=save_i.value+save_j.value;      ingredients.formula=save_i.formula+"+"+save_j.formula;      ingredients.formula_is_sum=true;      ingredients[j]=ingredients[n-1];      if(show_me_how_it_is_done(ingredients,n-1,value))        return true;            ingredients.value=save_i.value*save_j.value;      ingredients.formula=(save_i.formula_is_sum?("("+save_i.formula+")"):save_i.formula)+        "*"+(save_j.formula_is_sum?("("+save_j.formula+")"):save_j.formula);      ingredients.formula_is_sum=false;      if(show_me_how_it_is_done(ingredients,n-1,value))        return true;    }  }  return false;}int main() {  IntWithFormula d[4]={1,2,3,4};    for(int i=0;i<50;++i)    show_me_how_it_is_done(d,4,i);}

EDIT: I could add some comments, but you'll have more fun trying to figure out how it works. At least I gave most things reasonable names.

This topic is closed to new replies.

Advertisement