问题描述 | |
试题编号: | 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");
}
}
}
}