zoukankan      html  css  js  c++  java
  • 线性分类器

    依旧用的结构体。。。ps.好像是可以给一个输入进行一次输出的。

    //2020-6-1 线性分类器
    //给定一条直线,判断它是否能将训练数据中A,B两类点分开 
    #include<iostream>
    
    using namespace std;
    
    struct Point//
    {
    int x,y;//二维坐标 
    char type;//所属类别 
    };
    Point point[1001];
    
    struct Line//线
    {
    int a0,a1,a2;//a0+a1x+a2y=0
    int flag;//1-可以完美分割 0-不可完美分割 
    }; 
    Line line[21];
    
    int main()
    {
        int n,m;//n个点,m条线
        cin>>n>>m; 
        
        
        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 lineLoc;//-1线下 1线上
            
            cin>>line[i].a0>>line[i].a1>>line[i].a2;
            line[i].flag=1;//初始化 
            
            //由第一个点确定线上线下位置与所属类别的关系 
            if(line[i].a0+line[i].a1*point[0].x+line[i].a2*point[0].y>0)lineLoc=1;
            else if(line[i].a0+line[i].a1*point[0].x+line[i].a2*point[0].y<0)lineLoc=-1;
            
            for(int j=1;j<n;j++)
            {
                  if(lineLoc==1)
                  {
                      if(line[i].a0+line[i].a1*point[j].x+line[i].a2*point[j].y>0)
                      {
                          if(point[j].type!=point[0].type) line[i].flag=0; 
                    }
                    else if(line[i].a0+line[i].a1*point[j].x+line[i].a2*point[j].y<0)
                    {
                        if(point[j].type==point[0].type) line[i].flag=0;
                    }
                }
                else if(lineLoc==-1)
                {
                    if(line[i].a0+line[i].a1*point[j].x+line[i].a2*point[j].y<0)
                      {
                          if(point[j].type!=point[0].type) line[i].flag=0; 
                    }
                    else if(line[i].a0+line[i].a1*point[j].x+line[i].a2*point[j].y>0)
                    {
                        if(point[j].type==point[0].type) line[i].flag=0;
                    }
                }
            } 
            if(line[i].flag==1)cout<<"Yes"<<endl;
            else if(line[i].flag==0)cout<<"No"<<endl; 
        }
        return 0;
    } 
  • 相关阅读:
    laravel5.5
    yii2.0 Activeform表单部分组件使用方法
    putty连接远程局域网的MySql(不需要单独打开plink)
    关于 CentOS 自启动(服务、脚本)
    make -j 多核并行编译 导致笔记本过热 自动关机保护
    CentOS 7 引导 -- GRUB2
    Centos 7 拨号上网(PPPOE)
    Centos 7 意外断电如何处理
    Windows/Linux 生成iOS证书及p12文件
    git add -f
  • 原文地址:https://www.cnblogs.com/precious112/p/13720641.html
Copyright © 2011-2022 走看看