Reputation: 43
I'm solving this beginner problem on Codeforces, Young Physicist. https://codeforces.com/problemset/problem/69/A
The answer is to find if 3 forces are in equilibrium.
I've submitted two versions of my answers. One passed but one didn't. But I can't figure why it won't pass, aren't both of the condition same?
if (sumx == 0 && sumy == 0 && sumz == 0)
cout << "YES" << "\n";
else
cout << "NO" << "\n";
above is the one that passed.
if (sumx + sumy + sumz == 0)
cout << "YES" << "\n";
else
cout << "NO" << "\n";
this is the one that didn't pass.
Upvotes: 2
Views: 94
Reputation: 29193
Consider sumx = -1
, sumy = 1
, sumz = 0
. There's a 2-dimensional infinity of solutions to x + y + z = 0
, and x = y = z = 0
is just a 0-dimensional singularity.
If you graph x + y + z = 0
, you get this:
Every point on this plane is a set of three values that gives you 0 from their sum. The point where all three are zero is just a single point out of the sea. A quick check that tells you you've oversimplified the constraint is that you have 3 variables, but only 1 equation. That means you should still have 3 - 1 = 2 dimensions of freedom, but a single point has 0 dimensions of freedom.
Upvotes: 7
Reputation: 797
The answer is no. When you have sumx + sumy + sumz == 0
, it doesn't necessarily mean that all these variables are equal to 0. Let's assume that sumx = -1
, sumy = 0
, symz = 1
. This:
if (sumx == 0 && sumy == 0 && sumz == 0)
cout << "YES" << "\n";
else
cout << "NO" << "\n";
outputs NO
, but this:
if (sumx + sumy + sumz == 0)
cout << "YES" << "\n";
else
cout << "NO" << "\n";
outputs YES
.
Upvotes: 1