Jump to content
• Advertisement

# 10018

This topic is 3212 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 am trying to solve the ACM problem # 10018 LINK : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=959 I don't know why it is showing wrong answer.Can anyone help me to figure out the error?
#include<stdio.h>

int iteration = 0;

unsigned long int change(unsigned long int *n, unsigned long int *m)
{
unsigned long int x = *n;
while (x > 10)
{
*m += x%10;
*m *= 10;
x /= 10;
}
*m += x;

return 0;
}

unsigned long int palindrome(unsigned long int test_number)
{
unsigned long int rev_number=0, sum =0, rev_sum=0;
iteration++;
change(&test_number, &rev_number);
sum = test_number + rev_number;
change(∑ , &rev_sum);
if(sum != rev_sum)
palindrome(sum);
else
printf("%d %lu", iteration, sum);
return 0;
}
int main()
{
unsigned long int amount = 0, j, number=0;
scanf("%lu", &amount);
for(j=0; j<amount;j++)
{
scanf("%lu", &number);
palindrome(number);
iteration = 0;
printf("\n");
}
return 0;
}


#### Share this post

##### Share on other sites
Advertisement
change(∑ , &rev_sum);

I didn't know you could do that!

#### Share this post

##### Share on other sites
Quote:
 Original post by Alpha_ProgDeschange(∑ , &rev_sum);I didn't know you could do that!

Character Entities. An unintended side-effect of taking address of variable named sum.

& ¢ § ∑

Quote:
  *m += x%10; *m *= 10; x /= 10;

Instead of modifying m directly, use a temporary variable to reverse the digits.

#### Share this post

##### Share on other sites
Sorry that I'm not looking into it, but what does 3 seconds mean?
3 seconds to write the code :P

#### Share this post

##### Share on other sites
Quote:
 change(∑ , &rev_sum);

woops... :)

Quote:
 Instead of modifying m directly, use a temporary variable to reverse the digits.

I am always using another variable to hold the reverse value.I am just using m to access those values."rev_sum" and "rev_number"...arnt they?

#### Share this post

##### Share on other sites
Do you have to take into account negative numbers?

#### Share this post

##### Share on other sites
Check the condition for looping in change - it has a small mistake. After correcting it, your code is accepted by the online judge for me.

#### Share this post

##### Share on other sites
Thanks!. :D

I wonder why i didn't figure it out.

#### Share this post

##### Share on other sites
Quote:
 Original post by szecsSorry that I'm not looking into it, but what does 3 seconds mean?3 seconds to write the code :P

3 seconds for the code to execute. These limits are usually carefully chosen to make sure that you come up with a proper algorithm (in the big-O sense) and don't do anything especially stupid in the implementation of that algorithm, but generally don't require you to do optimization beyond that point.

Anyway. Have you tried actually using iteration instead of recursion - i.e., writing a loop? You might find it's a little simpler.

#### Share this post

##### Share on other sites
Quote:
Original post by Zahlman
Quote:
 Original post by szecsSorry that I'm not looking into it, but what does 3 seconds mean?3 seconds to write the code :P

3 seconds for the code to execute. These limits are usually carefully chosen to make sure that you come up with a proper algorithm (in the big-O sense) and don't do anything especially stupid in the implementation of that algorithm, but generally don't require you to do optimization beyond that point.
Thanks!

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5
• Advertisement

• 13
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632935
• Total Posts
3009304
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!