题目描述
在一个二维平面上有 n 个点,其中没有任何一个点位于 y 轴上。
请你判断这些点中是否存在一点满足,删除该点后,剩余的所有点都在 y 轴的同一侧。
输入格式
第一行包含整数 n。
接下来 n 行,每行包含两个整数 x,y,表示其中一个点的横纵坐标。
点的位置两两不重合。
输出格式
如果存在满足要求的点,则输出 Yes,否则输出 No。
数据范围
前三个测试点满足 2≤n≤10。
所有测试点满足 2≤n≤100,|x|,|y|≤100,|x|≠0。
样例
输入样例1:
3
1 1
-1 -1
2 -1
输出样例1:
Yes
输入样例2:
4
1 1
2 2
-1 1
-2 2
输出样例2:
No
输入样例3:
3
1 2
2 1
4 60
输出样例3:
Yes
思路:贪心,统计x的正负即可
时间复杂度 线性
参考文献
C++ 代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int a,b;
int t;
int main()
{
cin>>t;
while(t--)
{
int x,y;
cin>>x>>y;
if(x>0)a++;
else b++;
}
if(a==1&&b>a)puts("Yes");
else if(b==1&&a>b)puts("Yes");
else if(a==1&&b==1)puts("Yes");
else if(a==0&&b>0)puts("Yes");
else if(b==0&&a>0)puts("Yes");
else puts("No");
return 0;
}
java代码
import java.io.*;
import java.util.*;
public class Main {
static int a,b;
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int t=cin.nextInt();
while(t--!=0)
{
int x=cin.nextInt(),y=cin.nextInt();
if(x>0)a++;
else b++;
}
if (a <= 1 || b <= 1)System.out.println("Yes");
else System.out.println("No");
return ;
}
}
Python代码(大佬的)
def main():
n = int(input())
positive = 0
negative = 0
for _ in range(n):
x, y = map(int, input().split())
if x < 0:
negative += 1
elif x > 0:
positive += 1
if negative == 1 or positive == 1 or negative == 0 or positive == 0:
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
欢迎留言点赞
嗷嗷嗷