CSP-202006-1-线性分类器

CSP-202006-1-线性分类器

解题思路

  • 通过比较第一个训练数据点的类别和直线函数值的正负来确定标准类别和标准函数值的正负。
  • 循环遍历训练数据中的每个点,计算直线函数值并与标准函数值比较,以确定该点所在的类别。
    • 如果当前点的类别与标准类别一致,但直线函数值的正负与标准函数值不一致,或者当前点的类别与标准类别不一致,但直线函数值的正负与标准函数值一致,则更新isYes为0,表示该直线不能完美分开A、B两类点。
#include<iostream>
using namespace std; 

struct Triplets
{
    int x;
    int y;
    char type;
};

int main() {
    int n, m;
    cin >> n >> m;
    Triplets* point = new Triplets[n];

    for (int i = 0; i < n; i++)
    {
        cin >> point[i].x >> point[i].y >> point[i].type;
    }

    for (int i = 0; i < m; i++)
    {
        int theta_0, theta_1, theta_2, isYes = 1;
        cin >> theta_0 >> theta_1 >> theta_2;

        // 以第一个点为基准确定标准类别和标准函数值的正负
        char standard_type = point[0].type;
        bool standard_fx = (theta_0 + theta_1 * point[0].x + theta_2 * point[0].y) > 0;

        // 遍历训练数据点,判断是否能够完美分开
        for (int j = 1; j < n; j++)
        {
            bool fx = (theta_0 + theta_1 * point[j].x + theta_2 * point[j].y) > 0;
            
            if (point[j].type == standard_type)
            {
                if (fx != standard_fx)
                {
                    isYes = 0;
                    break;
                }
            }
            else
            {
                if (fx == standard_fx)
                {
                    isYes = 0;
                    break;
                }
            }
        }
        if (isYes) cout << "Yes\n";
        else cout << "No\n";
    }
    delete[] point;
    return 0;
}

请添加图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值