Jishan's Log 14: Codeforces 69A solution
Codeforces 69A: Young Physicist
The Problem:
A boy is given a boy with a set of forces acting on it in x, y and z space. He needs to find out if the body is in equilibrium or not.
For a body to be in equilibrium, all forces on it must be balanced ( Σ(F) == 0 ). So, we need to find out if the forces in x, y, z plane individually are 0.
The Solution:
The Algorithm:
1) Take n input. Declare sums of x, y, z and set them to 0.
2) Take a loop and make it run n times.
3) Take the x, y, z inputs and sum them up with their respective sum variables.
4) Check to see if all of them are zero. If true, print YES. Else print NO
The Code:
#include<bits/stdc++.h>
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
int main()
{
fastio;
int n; cin >> n;
int x_sum, y_sum, z_sum;
x_sum = y_sum = z_sum = 0;
while (n--)
{
int x, y, z; cin >> x >> y >> z;
x_sum += x;
y_sum += y;
z_sum += z;
}
if(x_sum == 0 && y_sum == 0 && z_sum == 0) cout << "YES\n";
else cout << "NO\n";
}
Comments
Post a Comment