Efficient way to do this

Started by
8 comments, last by xEricx 18 years, 8 months ago
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?
Advertisement
Ok a hint: std::stringstream.
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?
Bloats up your code?
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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 !!

----------------------------------------------------
Check out my casual TBS game blog
----------------------------------------------------
Quote:Original post by 6
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 !!


Thats how I had it originally, but it makes it messy when you are debugging. Anyway, I think I have it solved.
Never mind. Apparently, the USACO C++ compiler doesn't have a good implementation of stringstreams.
Could you use hexadecimal for each number?
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.
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

This topic is closed to new replies.

Advertisement