CSP认证2020-06-1-线性分类器-(Java)100分

线性分类器

问题描述
试题编号: 202006-1
试题名称: 线性分类器
时间限制: 1.0s
内存限制: 512.0MB
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Java满分答案
在这里插入图片描述
代码如下:

import java.util.Scanner;

class Point1{
    public int x;
    public int y;
    public char type;
}
class Line{
    public int a;
    public int b;
    public int c;
}
public class Main {
    public static void main(String [] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();    // n 个点
        int m = sc.nextInt();    // m 条线
        String[] str = new String[m];    // 输出结果

        Point1[] point = new Point1[n];
        for(int i = 0;i < n;i++){
            point[i] = new Point1();
            point[i].x = sc.nextInt();
            point[i].y = sc.nextInt();
            point[i].type = sc.next().charAt(0);
        }

        Line[] line = new Line[m];
        for(int i = 0;i < m;i++){
            line[i] = new Line();
            // a + b*x + c*y = 0
            line[i].a = sc.nextInt();
            line[i].b = sc.nextInt();
            line[i].c = sc.nextInt();

            // adec bdec 分别代表a, b在哪边 (1 是右或者下) (-1 是左或者上) 默认 a 左 b 右
            int adec = -1;
            boolean res = false;
            if(line[i].c == 0){    // x = -a/b
                double x = -(double)line[i].a/line[i].b;
                if(point[0].type == 'A' && point[0].x > x || point[0].type == 'B' && point[0].x < x)
                    adec = 1;
                for(int j = 1;j < n;j++){
                    if(adec == 1){ // a 在右边, b 在左边
                        if(point[j].type == 'A' && point[j].x < x || point[j].type == 'B' && point[j].x > x)
                            break;
                    }else{         // a 在左边, b 在右边
                        if(point[j].type == 'A' && point[j].x > x || point[j].type == 'B' && point[j].x < x)
                            break;
                    }
                    if(j == n-1)   // 都符合
                        res = true;
                }
                if(res == true)
                    str[i] = "Yes";
                else
                    str[i] = "No";
            } else {    // y = -(a + b*x)/c
                double y = -(double)(line[i].a + line[i].b*point[0].x)/line[i].c;
                if(point[0].type == 'A' && point[0].y > y || point[0].type == 'B' && point[0].y < y)
                    adec = 1;
                for(int j = 1;j < n;j++){
                    y = -(double)(line[i].a + line[i].b*point[j].x)/line[i].c;
                    if(adec == 1){      // a 在上边 b 在下边
                        if(point[j].type == 'A' && point[j].y < y || point[j].type == 'B' && point[j].y > y)
                            break;
                    } else {      // a 在下边 b 在上边
                        if(point[j].type == 'A' && point[j].y > y || point[j].type == 'B' && point[j].y < y)
                            break;
                    }
                    if(j == n-1)   // 都符合
                        res = true;
                }
                if(res == true)
                    str[i] = "Yes";
                else
                    str[i] = "No";
            }
        }
        for(int i = 0;i < m;i++)
            System.out.println(str[i]);

        sc.close();
    }
}
原文地址:https://www.cnblogs.com/jiaohuadehulike/p/14294996.html