Trick:

while(~scanf("%d %d", &a, &b))

**Explanation:**

In many problems we need to take input till EOF (End of File).

If we take input in c++ with scanf we can check if the return value of scanf is equal to -1 or equal to the number of input variables. Ok enough talk, we do one of the following most of the time

while(scanf("%d %d", &a, &b) == 2)

or

while(scanf("%d %d", &a, &b) != -1) //EOF == -1

Now we know that -1 means all 111111….1 in binary. So if we NOT -1 we get 0000000..0000 in binary which C/C++ detects as false.

So we can basically change

while(someValue != -1)

to

while(~someValue)

So we can write

while(~scanf("%d %d", &a, &b))

Thanks to yooo‘s code from codeforces

–Enzam

### Like this:

Like Loading...

*Related*

Pingback: Small Tips& Trics for Competitive Programming Contests | Enzam's Blog

wow! nice technique 🙂