Sign in to follow this  
iamcreasy

10018

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


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
change( , &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


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


Link to post
Share on other sites
Quote:
Original post by szecs
Sorry 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


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by szecs
Sorry 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


Link to post
Share on other sites
Quote:
Original post by xibo
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.


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

Quote:
Original post by Zahlman
Anyway. 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 this post


Link to post
Share on other sites
Quote:
Original post by iamcreasy
Quote:
Original post by Zahlman
Anyway. 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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this