Jump to content
  • Advertisement
Sign in to follow this  
Sagar_Indurkhya

Efficient way to do this

This topic is 4887 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

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 this post


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


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

Share this post


Link to post
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

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!