Solving Strange Addition Problem Differently
Tommorrow is my Operating System practical, going through them I realize that they are not too tough, so gave them a try and moved on. I am not in a mood to study right now, then it hit me why not try competitive programming ( meet my old darling) , so I decided to solve one of the easy problem, it was Strange additions. Problem itself was quite easy.
Given two numbers. First, reverse them, add them, reverse them again and print the answer
You might me thinking how my blog post changed itself from a narrative to talking one, never mind, that’s me, happens quite often in my writing. Plus dude, it’s an easy problem, there is nothing special about it that it deserves a blog post. Yes, I know but remember I changed it strangely. So pseudocode was like
Well, problem is trivial and my language of choice is C++ (modern C++). So insteading of doing it the old school way, i.e , looping and reversing the integers manually, I tried to make the compiler do the heavylifting for me (STL’s). Call me lazy or silly, instead of reversing the integers I converted them to strings and reversed the strings then converted the strings back to integers. I know that my answer is not perfect but Hey ! it works :) So my algo was something like this
Thankly, C++11 made the code smaller. For converting integers to string instead of using the stringstream or sstream, I used the C++11’s to_string method. For reversing the string I used the generic reverse() defined in the algorithm library and for coverting String back to integer instead of using String’s stoi I used C++11’s stoull (since I needed an unsigned long long integer). So here’s my final code. My thought : That looks like a badly styled functional paradigmed C++ :) Sumbmitted Code Link : Hacker Earth
Comments