ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1044. Lucky Tickets. Easy!

help,please,why my solution fail?
Posted by mythysjh 2 Sep 2011 14:53
here is my code:

#include <stdio.h>
int total(int count, int sum)
{
    int result = 0;
    if( count > 1 )
    {
        int i=0;
        for (; i <= 9; i++)
        {
            result += total(count - 1, sum     - i);
        }
    }
    else
    {
        return  sum     < 10 && sum >= 0 ? 1 : 0;
    }
    return result;
}

int main ()
{
    unsigned a;
    int n,i,c,b;

    scanf("%ud", &a);
    b = a / 2 * 9;
    for(i=0; i <= b; i++ )
    {
        c = total(a/2, i);
        n += c * c;
    }
    printf("%d", n);

    return 0;
}
i tried 2,4,6,8,the result is correct,but when i submit my solution,[wrong answer] is returned...

Edited by author 02.09.2011 14:58
Re: help,please,why my solution fail?
Posted by mythysjh 3 Sep 2011 06:58
i got it,in visual stdio 2010,auto variable must initialized before used
Re: help,please,why my solution fail?
Posted by Bahador Biglari 15 Dec 2011 12:18
Hello Smart :D,

Can you please explain how does your algorithm works? I find out that the summation of digits is multiplication of 9 at most; but why (c* c) ???? ( please explain the whole intention behind your algo as well.

Thanks
Re: help,please,why my solution fail?
Posted by killo 25 Jun 2012 16:20
Here is the correct code
#include <stdio.h>
int total(int count, int sum)
{
    int result = 0;
    if( count > 1 )
    {
        int i=0;
        for (; i <= 9; i++)
        {
            result += total(count - 1, sum     - i);
        }
    }
    else
    {
        return  sum     < 10 && sum >= 0 ? 1 : 0;
    }
    return result;
}

int main ()
{
    unsigned a;
    int n = 0;
    int i,c,b;

    scanf("%ud", &a);
    b = a / 2 * 9;
    for(i=0; i <= b; i++ )
    {
        c = total(a/2, i);
        n += c * c;
    }
    printf("%d", n);

    return 0;
}
Here is the correct code