- May 21, 2003
- 2,403
- 3
- 81
[Scroll down to post #9 for more current questions please, thanks for reading]
My goal is one problem per day on Project Euler and so far I'm on track. I may need to change that if they get too hard or I get too busy.
My code for Question 4 works pretty well. I initially wanted to write a function that took an integer, converted to a string and reversed it, returning it as an integer (for testing of palindromicity). The stuff I tried would often give errors like "invalid casting of int to char" or sometimes it would compile but on execution it raped my mom and laughed at me and I needed task manager to make it stop.
Ultimately I used a math based approach to reverse the digits like so:
(I confess, the code above came from http://www.cplusplus.com/forum/beginner/812/ ). I feel ok about using it because I can understand what it does.
My question is however, how might you do the int --> string --> int conversion? "cast..." and "static_cast..." or "char = someintegervarialbe;" eluded me somehow and my mom will never be the same.
My goal is one problem per day on Project Euler and so far I'm on track. I may need to change that if they get too hard or I get too busy.
My code for Question 4 works pretty well. I initially wanted to write a function that took an integer, converted to a string and reversed it, returning it as an integer (for testing of palindromicity). The stuff I tried would often give errors like "invalid casting of int to char" or sometimes it would compile but on execution it raped my mom and laughed at me and I needed task manager to make it stop.
Ultimately I used a math based approach to reverse the digits like so:
Code:
int revint(int sourcenum){
int temp = sourcenum;
int sum = 0;
while (temp){
sum *= 10;
sum += temp%10;
temp /= 10;
}
return sum;
}
My question is however, how might you do the int --> string --> int conversion? "cast..." and "static_cast..." or "char = someintegervarialbe;" eluded me somehow and my mom will never be the same.
Last edited: