当前位置:网站首页>线性分类器(CCF20200901)
线性分类器(CCF20200901)
2022-07-28 11:49:00 【.DoubleBean.】



样例输入
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
样例输出
No
No
Yes
思路:
先构建两个结构体数组。
typedef struct Node{
// x,y点坐标 F标记 -- 代入直线后结果小于0时,则将F赋值为-1,大于0则赋值为1
int x, y, F;
}Node;
Node flag1[maxn] = {
0 }, flag2[maxn] = {
0 };
首先读取数据,判断点坐标是属于A还是B,将他们分开存入两个结构体数组flag1,flag2之中。
// n个已知类别的点 m个查询直线方程表达式 i遍历用的变量 x,y点坐标 a,b,c直线方程的各个系数
int n, m, i, x, y, a, b, c;
char type;
string ss, line;
getline(cin, line);
stringstream L(line);
L >> n >> m;
for (i = 0; i < n;i++){
getline(cin, line);
stringstream ss(line);
ss >> x >> y >> type;
if (type == 'A'){
flag1[Alen].x = x;
flag1[Alen].y = y;
Alen++; //Alen来记录属于A的点坐标个数
}
else{
flag2[Blen].x = x;
flag2[Blen].y = y;
Blen++; //Blen来记录属于B的点坐标个数
}
}
将点的坐标代入直线方程,计算结果是大于0还是小于0,若遍历一遍flag1结构体数组中全部的点,它们全都是在直线的一侧,即F都是1,或者都是-1,表明属于A的点全部在直线一侧。否则在直线两侧,如果在直线两侧,这种情况下就不用判断flag2了,直接返回。
判断flag2结构体数组中全部的点是否在直线另一侧也是同样的原理。
int myfun(int a, int b, int c)
{
int i;
for (i = 0; i < Alen; i++){
if (i == 0){
if ((a + b * flag1[i].x + c * flag1[i].y) < 0){
flag1[i].F = -1;
}
else{
flag1[i].F = 1;
}
}
else{
if ((a + b * flag1[i].x + c * flag1[i].y) < 0 && flag1[i - 1].F == -1){
flag1[i].F = -1;
}
else if ((a + b * flag1[i].x + c * flag1[i].y) > 0 && flag1[i - 1].F == 1){
flag1[i].F = 1;
}
else{
break;
}
}
}
if (i != Alen) return -1;
for (i = 0; i < Blen; i++){
if (i == 0){
if ((a + b * flag2[i].x + c * flag2[i].y) < 0){
flag2[i].F = -1;
}
else{
flag2[i].F = 1;
}
}
else{
if ((a + b * flag2[i].x + c * flag2[i].y) < 0 && flag2[i - 1].F == -1){
flag2[i].F = -1;
}
else if ((a + b * flag2[i].x + c * flag2[i].y) > 0 && flag2[i - 1].F == 1){
flag2[i].F = 1;
}
else{
break;
}
}
}
if (i != Blen) return -1;
return 1;
}
最终代码如下:
#include<iostream>
#include<string>
#include<sstream>
const int maxn = 1010;
using namespace std;
int Alen = 0, Blen = 0;
typedef struct Node{
int x, y, F;
}Node;
Node flag1[maxn] = {
0 }, flag2[maxn] = {
0 };
int myfun(int a, int b, int c)
{
int i;
for (i = 0; i < Alen; i++){
if (i == 0){
if ((a + b * flag1[i].x + c * flag1[i].y) < 0){
flag1[i].F = -1;
}
else{
flag1[i].F = 1;
}
}
else{
if ((a + b * flag1[i].x + c * flag1[i].y) < 0 && flag1[i - 1].F == -1){
flag1[i].F = -1;
}
else if ((a + b * flag1[i].x + c * flag1[i].y) > 0 && flag1[i - 1].F == 1){
flag1[i].F = 1;
}
else{
break;
}
}
}
if (i != Alen) return -1;
for (i = 0; i < Blen; i++){
if (i == 0){
if ((a + b * flag2[i].x + c * flag2[i].y) < 0){
flag2[i].F = -1;
}
else{
flag2[i].F = 1;
}
}
else{
if ((a + b * flag2[i].x + c * flag2[i].y) < 0 && flag2[i - 1].F == -1){
flag2[i].F = -1;
}
else if ((a + b * flag2[i].x + c * flag2[i].y) > 0 && flag2[i - 1].F == 1){
flag2[i].F = 1;
}
else{
break;
}
}
}
if (i != Blen) return -1;
return 1;
}
int main(){
// n个已知类别的点 m个查询直线方程表达式 i遍历用的变量 x,y点坐标 a,b,c直线方程的各个系数
int n, m, i, x, y, a, b, c;
char type;
string ss, line;
getline(cin, line);
stringstream L(line);
L >> n >> m;
for (i = 0; i < n;i++){
getline(cin, line);
stringstream ss(line);
ss >> x >> y >> type;
if (type == 'A'){
flag1[Alen].x = x;
flag1[Alen].y = y;
Alen++; //Alen来记录属于A的点坐标个数
}
else{
flag2[Blen].x = x;
flag2[Blen].y = y;
Blen++; //Blen来记录属于B的点坐标个数
}
}
for (i = 0; i < m; i++){
getline(cin, line);
stringstream ss(line);
ss >> a >> b >> c;
if (myfun(a, b, c)>0) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
边栏推荐
- VS code更新后不在原来位置
- 一台电脑上 多个项目公用一个 公私钥对拉取gerrit服务器代码
- Multiple items on a computer share a public-private key pair to pull the Gerrit server code
- Custom paging tag 02 of JSP custom tag
- The usage and Simulation Implementation of vector in STL
- Developing NES game (cc65) 07 and controller with C language
- SuperMap iclient3d for webgl to realize floating thermal map
- AVL树(平衡搜索树)
- Under what circumstances can the company dismiss employees
- [nuxt 3] (XII) project directory structure 3
猜你喜欢

04 pyechars 地理图表(示例代码+效果图)

Initialization examples of several modes of mma8452q

The usage and Simulation Implementation of vector in STL

大模型哪家强?OpenBMB发布BMList给你答案!

Hongjiu fruit passed the hearing: five month operating profit of 900million Ali and China agricultural reclamation are shareholders

VS code更新后不在原来位置

GMT安装与使用

Markdown concise grammar manual

New progress in the implementation of the industry | the openatom openharmony sub forum of the 2022 open atom global open source summit was successfully held

Interface control telerik UI for WPF - how to use radspreadsheet to record or comment
随机推荐
The openatom openharmony sub forum was successfully held, and ecological and industrial development entered a new journey
DIY system home page, your personalized needs PRO system to meet!
上位机和三菱FN2x通信实例
C# 泛型是什么、泛型缓存、泛型约束
微创电生理通过注册:年营收1.9亿 微创批量生产上市企业
开源汇智创未来 | 2022 开放原子全球开源峰会 OpenAtom openEuler 分论坛圆满召开
[base] what is the optimization of optimization performance?
新零售电商O2O模式解析
Redis implements distributed locks
界面控件Telerik UI for WPF - 如何使用RadSpreadsheet记录或评论
[apue] 文件中的空洞
Unity加载Glb模型
Redis实现分布式锁
Jinshanyun rushes to the dual main listing of Hong Kong stocks: the annual revenue of 9billion is a project supported by Lei Jun
Hongjiu fruit passed the hearing: five month operating profit of 900million Ali and China agricultural reclamation are shareholders
Hc-05 Bluetooth module debugging slave mode and master mode experience
Developing NES games with C language (cc65) 04. Complete background
03 pyechars 直角坐标系图表(示例代码+效果图)
Zadig v1.13.0 believes in the power of openness, and workflow connects all values
scala 转换、过滤、分组、排序