欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java线性分类器

程序员文章站 2022-09-29 22:47:43
import java.util.Scanner;import java.util.ArrayList;public class Main { public static void main(String[] args) { ArrayList points_A = new ArrayList(); ArrayList points_B = new ArrayList

Java线性分类器
Java线性分类器
Java线性分类器

import java.util.Scanner;
import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        ArrayList<Point> points_A = new ArrayList<Point>();
        ArrayList<Point> points_B = new ArrayList<Point>();
        ArrayList<String> s = new ArrayList<String>();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        boolean temp;

        for(int i=0;i<n;i++){
            long x = sc.nextLong();
            long y = sc.nextLong();
            String type = sc.next();
            if(type.equals("A"))
                points_A.add(new Point(x,y,type));
            else if(type.equals("B"))
                points_B.add(new Point(x,y,type));
        }
        for(int i=0;i<m;i++){
            temp =true;
            long c = sc.nextLong();
            long a = sc.nextLong();
            long b = sc.nextLong();
            if(c+points_A.get(0).x*a+points_A.get(0).y*b==0||c+points_B.get(0).x*a+points_B.get(0).y*b==0)
                break;
            if(c+points_A.get(0).x*a+points_A.get(0).y*b>0) {
                for (int j = 1; j < points_A.size();j++ ) {
                    if (c + points_A.get(j).x * a + points_A.get(j).y * b <= 0) {
                        temp = false;
                        break;
                    }
                }
                if(temp==false){
                    s.add("No");
                    continue;
                }
                    for(int j=0;j<points_B.size();j++){
                        if(c+points_B.get(j).x*a+points_B.get(j).y*b>0){
                            temp = false;
                            break;
                        }
                    }
                if(temp==false){
                    s.add("No");
                    continue;
                }
            }
            else if(c+points_A.get(0).x*a+points_A.get(0).y*b<0){
                for (int j = 1; j < points_A.size();j++ ) {
                    if (c + points_A.get(j).x * a + points_A.get(j).y * b >= 0) {
                        temp = false;
                        break;
                    }
                }
                if(temp==false){
                    s.add("No");
                    continue;
                }
                    for(int j=0;j<points_B.size();j++){
                        if(c+points_B.get(j).x*a+points_B.get(j).y*b<0){
                            temp = false;
                            break;
                        }
                    }
                if(temp==false){
                    s.add("No");
                    continue;
                }
            }
            if(temp==true)
                s.add("Yes");
        }
        for(String ss: s){
            System.out.println(ss);
        }
    }
}
class Point{
    long x;
    long y;
    String type;
    Point(long x,long y,String type){
        this.x=x;
        this.y=y;
        this.type=type;
    }
}

本文地址:https://blog.csdn.net/Mrwyx/article/details/110291721