ccf_线性分类器

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

class Point{
	int x;
	int y;
	String type;
	Point(int x, int y, String type){
		this.x = x;
		this.y = y;
		this.type = type;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "(" + this.x + "," + this.y + "," + this.type + ")"; 
	}
}
class Line{
	int a;
	int b;
	int c;
	Line(int a, int b, int c){
		this.a = a;
		this.b = b;
		this.c = c;
	}
	@Override
	public String toString() {
		return "Line [a=" + a + ", b=" + b + ", c=" + c + "]";
	}
}

public class LinearClassifier {
	public static void main(String[] args) throws IOException {
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		String str = input.readLine().trim();
		String[] arr = str.split(" ");
		int n = Integer.valueOf(arr[0]);
		int m = Integer.valueOf(arr[1]);
		// 输入n个点
		List<Point> points = new ArrayList<>();
		for(int i = 0; i < n; ++i) {
			str = input.readLine().trim();
			arr = str.split(" ");
			points.add(new Point(Integer.valueOf(arr[0]), 
					Integer.valueOf(arr[1]), arr[2]));
		}
		
		List<Line> lines = new ArrayList<Line>();
		for(int i = 0; i < m; ++i) {
			str = input.readLine().trim();
			arr = str.split(" ");
			lines.add(new Line(Integer.valueOf(arr[0]), 
					Integer.valueOf(arr[1]), Integer.valueOf(arr[2])));
		}
		List<String> ans = new ArrayList<String>();
		for(Line temp_l: lines) {
			boolean flag = (temp_l.a + temp_l.b * points.get(0).x + temp_l.c * points.get(0).y) > 0;		
			String type = points.get(0).type;
			boolean check_flag = true;
			for(int i = 1; i < n; ++i) {
				boolean tmp_flag = (temp_l.a + temp_l.b * points.get(i).x + temp_l.c * points.get(i).y) > 0;
				if((!points.get(i).type.equals(type) && tmp_flag != flag)
						|| (points.get(i).type.equals(type) && tmp_flag == flag)) {
					continue;
				}
				check_flag = false;
				break;
			}
			if(!check_flag) {
				ans.add("No");
			}else {
				ans.add("Yes");
			}
		}
		for(int i = 0; i < m; ++i) {
			System.out.println(ans.get(i));
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值