【CSP】线性分类器

问题描述
试题编号: 202006-1
试题名称: 线性分类器
时间限制:1.0s
内存限制:512.0MB
问题描述:

题解:

import java.util.Scanner;

/**
 * @author Vigilr
 * @since 2020-08-06
 */
public class 线性分类器 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        //data存储坐标,type存取对应的类型
        int[][] data = new int[n][2];
        char[] type = new char[n];
        for (int i = 0; i < n; i++) {
            data[i][0] = scanner.nextInt();
            data[i][1] = scanner.nextInt();
            type[i] = scanner.next().charAt(0);
        }

        //line存取直线上的每个参数,results存取直线是否满足条件
        int[] line = new int[3];
        boolean[] results = new boolean[m];

        //up是指A类型是否在直线上方
        boolean up = true;

        for (int i = 0; i < m; i++) {
            //获取每条直线三个参数的输入
            for (int j = 0; j < 3; j++) {
                line[j] = scanner.nextInt();
            }

            //判断第一个点在直线的位置
            int result = line[0] + (line[1] * data[0][0]) + (line[2] * data[0][1]);
            if (type[0] == 'A' && result > 0) {
                up = true;
            } else if (type[0] == 'B' && result < 0) {
                up = true;
            } else {
                up = false;
            }

            //从第二个点开始判断
            for (int k = 1; k < n; k++) {
                result = line[0] + (line[1] * data[k][0]) + (line[2] * data[k][1]);
                if (up) {
                    if (type[k] == 'A' && result > 0) {
                        results[i] = true;
                    } else if (type[k] == 'A' && result < 0) {
                        results[i] = false;
                        break;
                    } else if (type[k] == 'B' && result > 0) {
                        results[i] = false;
                        break;
                    } else if (type[k] == 'B' && result < 0) {
                        results[i] = true;
                    }
                } else {
                    if (type[k] == 'A' && result > 0) {
                        results[i] = false;
                        break;
                    } else if (type[k] == 'A' && result < 0) {
                        results[i] = true;
                    } else if (type[k] == 'B' && result > 0) {
                        results[i] = true;
                    } else if (type[k] == 'B' && result < 0) {
                        results[i] = false;
                        break;
                    }
                }
            }
        }

        for (int i = 0; i < m; i++) {
            if (results[i]) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }
}

更多题解的源码已上传至GitHub码云,欢迎访问

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值