Cash Change

Limits 1s, 512 MB

You have an unlimited number of cash notes of the following denominations: 1, 5, 10, 50, 100, 500.


This is a companion discussion topic for the original entry at https://toph.co/p/cash-change
#include<stdio.h>
#include<math.h>
int main()
{
    int i,n,w=0,x=0,y=0,z=0,t;
    scanf("%d",&n);
    if(n>=500)
    {
        z=n/500;
        n=n%500;
    }
    if(n>=100 && n<500)
    {
        y=n/100;
        n=n%100;
    }
    if(n>=50 && n<100)
    {
        x=n/50;
        n=n%50;
    }
    if(n>=10 && n<50)
    {
        w=n/10;
        n=n%10;
    }
    if(n>=5 && n<10)
    {
        if(n==5)
        {
            printf("%d ",5);
        }
        else
        {
            t=n/5;
            n=n%5;
        }
    }
    if(n<5)
    {
        for(i=1;i<=n;i++)
            printf("%d ",1);
    }
    if(t<2)
    {
        for(i=1;i<=t;i++)
         {printf("%d ",5);}
    }
    if(w<=4)
    {
        for(i=1;i<=w;i++)
        {
            printf("%d ",10);
        }
    }
    if(x==1)
    {
        for(i=1;i<=x;i++)
        {
            printf("%d ",50);
        }
    }
    if(y<=4)
    {
        for(i=1;i<=y;i++)
        {
            printf("%d ",100);
        }
    }
    if(z<=20)
    {
        for(i=1;i<=z;i++)
        {
            printf("%d ",500);
        }
    }
    printf("\n");
    return 0;

}

What is the problem?wa in given test case.

1 Like

@Imran02 Looks like you found the fix for your problem. Nice work!

No,I didn’t fix.When I run the code in my compiler it works properly,but when i compile it in Toph it makes WA for given test case.

You are using t here in comparison. But you have never set the value of t. This will produce undefined behavior.

If you can explain your approach in words, I can help you guide through the fixes.

OK,thank you.Now it is accepted.

1 Like