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 1200. Horns and Hoofs

Judge, Please, Help! Why fail in test #2? this is code
Posted by Alexander J. Villalba G. 17 Apr 2020 02:34
I don't understand why my code fails. I have been very careful with all cases. The precision of the numbers, the zero and negative benefits, everything. My code should not have so many lines but I have put them for the frustration of having so many WA. Are you sure test # 2 is ok? Check it with my code. Previously I tried it with a much shorter code that reached test # 7 and failed, but it did not take care of rounding problems, this does and fails in test # 2 X-¿
Help, Please!


// Brute force
#include <stdio.h>
#include <math.h>

long calcMax (int horn_n , int hoof_n,  float horn_b, float  hoof_b ) {

    long max =  (long) horn_b * horn_n + (long) hoof_b * hoof_n - (long) 100 * ( horn_n * horn_n +  hoof_n * hoof_n);
    return max;
}

int passtoInt( char *number) {
    int num = 0, signo = 1, begin = 0;
    char decimal_flag = 'N';
    int dec_ndig = 0;
    if( number[0] == '-')  {
        signo = -1;
        begin = 1;
    }
    for(int i=begin; number[i] != 0; i++) {
        if(number[i] == '.')  {
            decimal_flag = 'Y' ;
            continue;
        }
        if(decimal_flag == 'Y') dec_ndig++;
        if(dec_ndig > 2) break;

        num = num * 10  + (number[i] - '0');

    }
    for(int i = dec_ndig; i < 2; i++) num = num*10;

    return num * signo;
}


void getPartEntAndDec(long num, long *ent, unsigned *dec )  {
    *ent = num / 100;
    *dec = num % 100;

}


int main(void) {
    char horn_s[14],  hoof_s[14];
    int  horn_n= 0, hoof_n = 0, horn = 0, hoof = 0;

    int horn_b, hoof_b;
    long max = 0, max1, max_ent;
    unsigned max_dec;
    int maxmount;

    scanf("%s%s%d", horn_s, hoof_s, &maxmount);
    horn_b = passtoInt( horn_s);
    hoof_b = passtoInt( hoof_s);

    for(int horn_n = 0; horn_n <= maxmount; horn_n++) {
        for(int hoof_n = 0; hoof_n <= maxmount -  horn_n;  hoof_n++) {
            max1 = calcMax (horn_n , hoof_n,  horn_b,  hoof_b );
            if(max1 > max) {
                horn = horn_n;
                hoof = hoof_n;
                max = max1;
            }
        }
    }
    getPartEntAndDec(max, &max_ent, &max_dec );
    printf("%ld.%02u\n", max_ent, max_dec);
    printf("%d %d\n", horn,  hoof);
    return 0;

}