删点--统计x坐标正负

题目描述
在一个二维平面上有 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()

欢迎留言点赞

嗷嗷嗷

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值