Блог пользователя Guaguapi

Автор Guaguapi, история, 5 лет назад, По-английски

Here is the problem 1221 F

Here is the code : Accepted ; Wrong answer ;

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
5 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

it also give same wrong answer for C++11

your 'tot' operation look unbehaviour

it should be

X[tot + 1] = X[tot] + 1, tot++;

»
5 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I could not find any operation for this behaviour.

Since it failed on 2nd test case so you can put debug statement with condition (n==5) so it won't fail on 1 case and you can know where it start changing behaviour.

If you find the mistake please update in post as well, i would like to know where it fail

  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    See This answer in stack-overflow. In your submission X[++tot] = X[tot-1] + 1; is undefined behavior for c++ versions less than c++17: The left hand side of the = operator can be executed before or after the right hand side. In c++17, it's guaranteed that the right side will be executed before the left side. I think you assumed that in X[tot-1] tot's value was increased by 1 because of X[++tot], but actually tot is increasing later.

    Thanks again ShafinKhadem !

»
5 лет назад, # |
  Проголосовать: нравится +18 Проголосовать: не нравится

See This answer in stack-overflow. In your submission X[++tot] = X[tot-1] + 1; is undefined behavior for c++ versions less than c++17: The left hand side of the = operator can be executed before or after the right hand side. In c++17, it's guaranteed that the right side will be executed before the left side. I think you assumed that in X[tot-1] tot's value was increased by 1 because of X[++tot], but actually tot is increasing later.