We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Mathematics
- Geometry
- Points On a Line
- Discussions
Points On a Line
Points On a Line
Sort by
recency
|
47 Discussions
|
Please Login in order to post a comment
!/bin/python3
import math import os import random import re import sys
if name == 'main': n = int(input()) H = [] V = []
My javascript solution (two liner, could easily be one):
int main() { int n,x,y,x1,y1,cx=0,cy=0,flag=0,num; scanf("%d",&n); num=n; while(n--) { if(flag==0) { scanf("%d%d",&x,&y); x1=x; y1=y; flag=1; } else if(flag==1) { scanf("%d%d",&x,&y); if(x==x1) { cx++; } else if(y==y1) { cy++; } } } if(cx==num-1 || cy==num-1) { printf("YES"); } else { printf("NO"); } }
The program reads an integer n, which represents the number of points to be checked.
It initializes variables num to store the original number of points, cx (count in x-direction), cy (count in y-direction), and flag to 0.
It enters a loop that iterates n times to read the coordinates of the points.
In the loop, if flag is 0, it reads the first set of coordinates (x, y), stores them in x1 and y1, and sets flag to 1 to indicate the first point has been read.
In subsequent iterations (when flag is 1), it reads the coordinates of the next point (x, y).
It checks if the x-coordinates of the points are the same, incrementing cx if they are. It checks if the y-coordinates of the points are the same, incrementing cy if they are.
After processing all points, it checks whether either cx (all points are on a horizontal line) or cy (all points are on a vertical line) is equal to num - 1. If either of these conditions is met, it prints "YES." Otherwise, it prints "NO."
The program determines if the given points lie on a straight line, either horizontally or vertically, and then provides the appropriate "YES" or "NO" output based on that determination.