【CCF】202006-1 线性分类器(Java实现)
试题编号: 202006-1
试题名称: 线性分类器
时间限制: 1.0s
内存限制: 512.0MB
问题描述:
样例输入1
9 3
1 1 A
1 0 A
1 -1 A
2 2 B
2 3 B
0 1 A
3 1 B
1 3 B
2 0 A
0 2 -3
-3 0 2
-3 1 1
思路
1.AB点分别存储
2.循环遍历,将点带入方程,结果>0或<0,而A>0则B<0,所以,循环listA判断结果>0则A++,循环Blist,结果>0则B++,如果listA全部>0,listB全部不>0或者listA全部不>0,listB全部>0,该方程为线性分类器
代码
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
// int []A = new int[][2];
ArrayList<int[]> listA = new ArrayList<>();
ArrayList<int[]> listB = new ArrayList<>();
int n = sc.nextInt();
int m = sc.nextInt();
String []res = new String[m];
for (int i = 0; i < n ; i++) {
int []xy = new int []{sc.nextInt(),sc.nextInt()};
String str = sc.next();
if (str.equals("A")){
listA.add(xy);
}else{
listB.add(xy);
}
}
int a = listA.size();
int b = listB.size();
for (int i = 0; i < m ; i++) {
int q0 = sc.nextInt();
int q1 = sc.nextInt();
int q2 = sc.nextInt();
int A = 0,B=0;
for (int j = 0; j < a; j++) {
int sum = q0 + q1*listA.get(j)[0] + q2*listA.get(j)[1];
if (sum>0){
A++;
}
}
for (int j = 0; j < b; j++) {
int sum = q0 + q1*listB.get(j)[0] + q2*listB.get(j)[1];
if (sum>0){
B++;
}
}
if ((A==0&&B==b)||(A==a&&B==0)){
res[i] = "Yes";
}else{
res[i] = "No";
}
}
for (int i = 0; i < m ; i++) {
System.out.println(res[i]);
}
}
}