# Efficient way to do this

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

## Recommended Posts

I've been working on a programming competition problem. Don't worry, its training, so as long as I don't ask for actual code, its fine. Now the actual question I have solved, but the easy part, outputting the answer, is bugging me, because my approach is really big and inefficient. Basically, I have a 4 list's of integers. The size of the lists are all the same, and the contents of the integers are between 1 and 13. Here: list<int> a looks like: <7, 9, 2, 4, 8, 3> I need to make an integer that is all the contents mashed. Here: From the above example -> 792483 Now I need to turn all the lists into integers like that, and then print out the first 3, from least to greatest. My approach was working fine when working with single digit integers, but now it is messing up. Anyone know a good way to do this?

##### Share on other sites
Ok a hint: std::stringstream.

##### Share on other sites
Yeah, I'm already using std::stringstream. But it still bloats my code up pretty bad. Anyone know any short cuts I don't see?

##### Share on other sites
You could multiply each integer by a power of 10 incremental, and then add them all up. i.e.

7 * 100000
9 * 10000
2 * 1000
4 * 100
8 * 10
3 * 1 // don't do this one

So you have 700000 + 90000 + 2000 + 400 + 80 + 3 = 792483. This will work if the integer is more than 9.

You may have to use a long long int incase the all teh 7 numbers are 13 !!

##### Share on other sites
Quote:
 Original post by 6You could multiply each integer by a power of 10 incremental, and then add them all up. i.e.7 * 1000009 * 100002 * 10004 * 1008 * 103 * 1 // don't do this oneSo you have 700000 + 90000 + 2000 + 400 + 80 + 3 = 792483. This will work if the integer is more than 9.You may have to use a long long int incase the all teh 7 numbers are 13 !!

Thats how I had it originally, but it makes it messy when you are debugging. Anyway, I think I have it solved.

##### Share on other sites
Never mind. Apparently, the USACO C++ compiler doesn't have a good implementation of stringstreams.

##### Share on other sites
Could you use hexadecimal for each number?

##### Share on other sites
Sorry everyone. It turns out that I had no need to use any strings in the program in the first place! Well, thanks for the help though.

##### Share on other sites
I might be wrong, but since you can have something like :

2, 13

using :

2 * 10
13 * 1

as proposed by 6 won't give you the right result, it will output 33 instead of 213

What you can do is something like :

Damn Source ... the += wont show...
Edit: seems like the += won't show only in preview... that's weird

int _tmain(int argc, _TCHAR* argv[]){    const int TestArray[] = { 6, 10, 1, 13, 4, 7 };    const int Size = sizeof(TestArray) / sizeof(TestArray[0]);    unsigned int FinalInt = 0;    for(int i = 0; i < Size; ++i)    {        const int CurrentInt = TestArray;        printf("Adding %d\n", CurrentInt);        int Multiplier = 10;        while(CurrentInt >= Multiplier)        {            Multiplier *= 10;        }        FinalInt *= Multiplier;        FinalInt += CurrentInt;    }    printf("Final Number: %d\n", FinalInt);	return 0;}

Just be sure not to get values over FinalInt
Edit: I meant over the maximum value that FinalInt can reach

Hope this helps...

Eric

1. 1
2. 2
3. 3
frob
15
4. 4
5. 5

• 20
• 12
• 13
• 14
• 80
• ### Forum Statistics

• Total Topics
632144
• Total Posts
3004411

×