Jump to content
  • Advertisement
Sign in to follow this  
falola45

Problem

This topic is 3639 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!