AzizbekSabirov's blog

By AzizbekSabirov, history, 86 minutes ago, In English

While solving 66A - Petya and Java, I compared the input string digit-by-digit against each data type's maximum limit ("127", "32767", "2147483647", "9223372036854775807").

  1. First, I submitted 396066326 and got WA 30 (2055050055) because my loop only checked if s[i] was greater than the limit digit, forgetting to stop and return early when s[i] was smaller.
  2. I fixed that bug only for int and submitted 396066667, which got WA 79 (2158324958633591462) due to the exact same bug in the long block.
  3. I then fixed long as well, leaving byte and short completely unpatched. Out of curiosity, I submitted it expecting another WA — but it got Accepted!

Why my AC code should actually get WA:

In my accepted code, the loops for byte and short still lack the early exit when a digit is smaller than the boundary digit:

for(int i = 0; i < s.size(); i++){
    if(int(s[i]) > int(byte[i])){
        cout << "short";
        return;
    }
    // Missing: else if (int(s[i]) < int(byte[i])) { cout << "byte"; return; }
}
cout << "byte";

Counterexample for byte (127): For input 118, at index 1 the digit '1' is smaller than '2', so the loop should stop and output byte. Instead, it continues to index 2, sees that '8' is greater than '7', and wrongly prints short.

Counterexample for short (32767): For input 29999, the first digit '2' is smaller than '3', but on the second digit '9' is greater than '2', so my code wrongly prints int instead of short.

The test suite catches this bug for int and long, yet completely misses simple cases like 118 and 29999 for byte and short!

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
10 minutes ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

bashe