题目

202006-01线性分类器 - 图1202006-01线性分类器 - 图2202006-01线性分类器 - 图3

思路

判定点集合是否都在同一侧,可以将点代入解析式,如果都>0或都<0则在同一侧。

….逻辑不难,但TM的要注意,标准输出是‘Yes’,不是’YES’!!害得我一直错!!

代码

  1. #include<iostream>
  2. #include<vector>
  3. #include<string.h>
  4. using namespace std;
  5. struct point{
  6. int x,y;
  7. char type;
  8. };
  9. struct line{
  10. int theta0,theta1,theta2;
  11. };
  12. vector<point> points;
  13. line lines[21];
  14. int count_A=0,count_B=0;
  15. int main(){
  16. memset(lines,0,sizeof(line));//初始化数组
  17. int n,m;
  18. cin>>n>>m;
  19. for(int i=0;i<n;i++){
  20. point temp_point;
  21. cin>>temp_point.x>>temp_point.y>>temp_point.type;
  22. if(temp_point.type =='A'){
  23. count_A++;
  24. }else{
  25. count_B++;
  26. }
  27. points.push_back(temp_point);
  28. }
  29. for(int j=0;j<m;j++){
  30. line temp_line;
  31. cin>>temp_line.theta0>>temp_line.theta1>>temp_line.theta2;
  32. lines[j] =temp_line;
  33. }
  34. for(int j=0;j<m;j++){
  35. int A1=0,A0=0,B1=0,B0=0;
  36. for(int i=0;i<n;i++){
  37. int result =lines[j].theta0+lines[j].theta1*points[i].x+lines[j].theta2*points[i].y;
  38. if(points[i].type =='A'){
  39. if(result>=0){
  40. A1++;
  41. }else{
  42. A0++;
  43. }
  44. //continue;
  45. } else if(points[i].type =='B'){
  46. if(result>=0){
  47. B1++;
  48. }else{
  49. B0++;
  50. }
  51. //continue;
  52. }
  53. }
  54. if(A0==count_A&&B1==count_B){
  55. //A全小于0,B全大于0
  56. cout<<"Yes"<<endl;
  57. continue;
  58. }
  59. if(A1==count_A&&B0==count_B){
  60. //A全大于0,B全小于0
  61. cout<<"Yes"<<endl;
  62. continue;
  63. }
  64. cout<<"No"<<endl;
  65. }
  66. return 0;
  67. }