When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

pcumamahesh's blog

By pcumamahesh, history, 10 months ago, In English

Hello. I recently stumbled upon this error which I thought was not the cause of my code failing in this problem:1695B - Circle Game.

My old submission: 207490372 (GNU C++ 17, WA).

My latest submission: 207493578 (GNU C++ 17, AC).

The subtle difference between these two pieces of code is that the else-statement was replaced with an if-statement, with the exact opposite condition of the if-statement above it. Also, i % 2 can either be 0 or 1 only. If that's not it, please correct me where I am going wrong (the error is somewhere else).

  • Vote: I like it
  • -3
  • Vote: I do not like it

»
10 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

In case of,

if (condition) {
}
else (condition) {
}

the else condition will only be checked when the if-condition is false.

But for

if (condition){
}
if (condition){
}

the two if conditions are independent. The first if condition has no effect on the second if condition. Both of them will be checked. This might be the case.

  • »
    »
    10 months ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Yeah I did think of that, but still, the two if conditions are the exact opposite; only one will be checked during each iteration I feel.

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

From WA solution:

if(!(i % 2))
            if(a[i] < sm)
            {
                sm = a[i];
                m = i;
            }
        
        else
            if(a[i] < sj)
            {
                sj = a[i];
                j = i;
            }

else refers to inner if, not to outer

  • »
    »
    10 months ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Ah I see. This was a mess of brackets after all. Thanks for correcting me :D