this program basically asks the user to input an integer. Then, the program should decompose that integer into integers which are powers of 3. for example:
I had something really long typed out... Then my browser crashed and lost it..Anyways it was something along the ling of you should look for values which are closest and work with them and not pick random values.
short example
Notes: equal or less = add next
greater = subtract next
123
closest is 81 (it is less so we add) current result: 81
distance = 123 - 81 = 42
closest is 27 (it is less so we add) current result: 81 + 27 = 108
distance = 42 - 27 = 15
closest is 9(it is less so we add) current result: 81 + 27 + 9 = 117
distance = 15 - 9 = 6
closest is 3(it is less so we add, we choose smallest of the closest to avoid subtracting) current result: 81 + 27 + 9 + 3 = 120
distance = 6 - 3 = 3
closest is 3(it is equal so we add) current result: 81 + 27 + 9 + 3 + 3 = 123
Coding this shouldn't be too difficult.
*eidt when I say add/subtract I mainly mean flip the next operation (starting with add)
What kind of structures can I use to code this? and is it necessary to use modulus operator: % to accomplish this?
Edit: Sorry but I still don't understand how will I use the information you put and code it. I just don't understand the concept I guess. Can you explain it differently? Sorry for holding you up. And I really really appreciate your help!
It's not very clean and might have a few bugs but I put this together pretty quickly so sorry ahead of time. Hopefully you can use some of this to work on yours. Please don't just copy paste it would be nice if you learned from it.
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <sstream>
int main()
{
int result;
std::cout << "Please enter the desired result: ";
std::cin >> result;
std::vector<int> const table = {1, 3, 9, 27, 81}; //set might be better but oh well
std::string answer = "";
int temp = result;
bool add = true;
while(temp)
{
int distance = std::abs(temp - 1); //you can get absolute manually really easy
int closest = 1;
for(std::size_t i = 1; i < table.size(); ++i) //might be able to get away with less iterations but its only 4 so no big deal
{
int tempDistance = std::abs(temp - table[i]);
if(distance && distance > tempDistance) //make sure we didn't find a perfect match and its decreasing distance
{
distance = tempDistance;
closest = table[i];
}
else
{
break; //getting farther away or we found a match no need to continue
}
}
if(closest > temp)
{
add = !add;
}
temp = distance;
if(temp) //the last number finished equation so don't putt sign at end
{
std::ostringstream ss;
ss << closest;
answer += ss.str();
answer += add ? '+' : '-';
}
else
{
std::ostringstream ss;
ss << closest;
answer += ss.str();
}
}
std::cout << result << " = " << answer << std::endl;
}
I had to do a similar program last semester but had to account for -121 to -1 too. How would you account from starting with an integer from the negative side?
You may want to create your own thread. Anyways, It would pretty much be the same logic but instead of adding up to a positive number you are now adding up to a negative.