chording   2년 전

거리가 1부터 30 정도로 작을 때 정답 올리신 분이 계시길래 비교해봐도 맞고,

거리가 최댓값 2^31-1일 때도 정상적으로 92681을 출력하는데

도대체 어디가 문제인지 모르겠습니다,,

zenith82114   2년 전

15번 줄에 UB가 포함되어 있습니다. 한 문장 안에서 같은 변수에 대한 ++/-- 연산자를 2개 이상 사용하면 안 됩니다.

https://en.cppreference.com/w/...

---

Undefined behavior

1) If a side effect on a memory location is unsequenced relative to another side effect on the same memory location, the behavior is undefined.

i = ++i + 2;       // well-defined
i = i++ + 2;       // undefined behavior until C++17
f(i = -2, i = -2); // undefined behavior until C++17
f(++i, ++i);       // undefined behavior until C++17, unspecified after C++17
i = ++i + i++;     // undefined behavior

2) If a side effect on a memory location is unsequenced relative to a value computation using the value of any object in the same memory location, the behavior is undefined.

cout << i << i++; // undefined behavior until C++17
a[i] = i++;       // undefined behavior until C++17
n = ++i + i;      // undefined behavior

chording   2년 전

헉 감사합니다!!!!!!

그런 규칙이 있는지 몰랐네요

댓글을 작성하려면 로그인해야 합니다.