ATM (HS08TEST) Wrong Answer

I know that many beginners have issues with this problem, and I was able to solve it by using


s instead of using the below solution, but I don’t understand why the following solution (submitted solution here) received “Wrong Answer”:

#include <stdio.h>
#include <stdlib.h>

typedef short withdrawl;
typedef short parts;
typedef short deposit;

int main() {
    withdrawl num;
    parts beg, end;
    deposit total;
    scanf("%hi %hi.%hi", &num, &beg, &end);
    total = 100*beg+end;
    if (!(num % 5)) {
        total -= num*100+50;
        if (total < 0) total = 100*beg+end;
    printf("%hi.%hhi%hhi\n", total/100, (total % 100)/10, total % 10);

I would really appreciate if someone could explain why the above solution didn’t pass. Thank you!

Maximum value short int could accept is 32767(=> 2^15 - 1). If the input is near to 2000(max of X and Y), in your code, num*100 or beg*100 would cross the max value of short causing a range overflow.

Try input

1000 2000

Answer should be 999.50. But your code outputs some other value.

1 Like