# 10018

This topic is 2944 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
change(∑ , &rev_sum);

I didn't know you could do that!

##### 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 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 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 on other sites
Do you have to take into account negative numbers?

##### 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 on other sites
Thanks!. :D

I wonder why i didn't figure it out.

##### 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 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 on other sites
the runtime is indeed bad - however, back when i was active at uva-acm the submits were rejected with RTE instead of WA if they took too long to compute.

##### Share on other sites
Quote:
 Original post by xibothe runtime is indeed bad - however, back when i was active at uva-acm the submits were rejected with RTE instead of WA if they took too long to compute.

isn't that why they have included Time Limit Exceeded.

Quote:
 Original post by ZahlmanAnyway. Have you tried actually using iteration instead of recursion - i.e., writing a loop? You might find it's a little simpler.

could u plz show me an example...i havnt dont anything in that way.

[Edited by - iamcreasy on December 31, 2009 3:38:06 PM]

##### Share on other sites
Quote:
Original post by iamcreasy
Quote:
 Original post by ZahlmanAnyway. Have you tried actually using iteration instead of recursion - i.e., writing a loop? You might find it's a little simpler.

could u plz show me an example...i havnt dont anything in that way.

Just change the recursion in palindrome to use iteration (with a do/while loop) instead:

void palindrome(unsigned long int test_number){    unsigned long int rev_number=0, sum =0, rev_sum=0;    do {        iteration++;        change(&test_number, &rev_number);        sum = test_number + rev_number;        change(&sum , &rev_sum);    } while(sum != rev_sum);        printf("%d %lu", iteration, sum);}

Now that I notice it, it looks like your code doesn't handle numbers that are already palindromic.. I guess they don't have a very good input test data set :)