当前位置:网站首页>Linear classifier (ccf20200901)
Linear classifier (ccf20200901)
2022-07-28 12:45:00 【.DoubleBean.】



The sample input
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
Sample output
No
No
Yes
Ideas :
First build two structural arrays .
typedef struct Node{
// x,y Point coordinates F Mark -- After substituting into the straight line, the result is less than 0 when , Will F The assignment is -1, Greater than 0 The assignment is 1
int x, y, F;
}Node;
Node flag1[maxn] = {
0 }, flag2[maxn] = {
0 };
First read the data , Judge whether the point coordinates belong to A still B, Store them separately into two structural arrays flag1,flag2 In .
// n Points of known categories m Query linear equation expression i Variables for traversal x,y Point coordinates a,b,c The coefficients of the linear equation
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 To record belonging to A Number of point coordinates
}
else{
flag2[Blen].x = x;
flag2[Blen].y = y;
Blen++; //Blen To record belonging to B Number of point coordinates
}
}
Substitute the coordinates of points into the linear equation , The calculation result is greater than 0 Still less than 0, If you traverse flag1 All points in the structure array , They are all on the side of the straight line , namely F All are 1, Or both -1, Indicate belonging to A All points of are on the side of the straight line . Otherwise, on both sides of the line , If on both sides of the line , In this case, there is no need to judge flag2 了 , Go straight back to .
Judge flag2 Whether all points in the structure array are on the other side of the line is the same principle .
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;
}
The final code is as follows :
#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 Points of known categories m Query linear equation expression i Variables for traversal x,y Point coordinates a,b,c The coefficients of the linear equation
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 To record belonging to A Number of point coordinates
}
else{
flag2[Blen].x = x;
flag2[Blen].y = y;
Blen++; //Blen To record belonging to B Number of point coordinates
}
}
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;
}
边栏推荐
- [half understood] zero value copy
- 行业落地呈现新进展 | 2022 开放原子全球开源峰会 OpenAtom OpenHarmony 分论坛圆满召开
- Why do enterprises need the ability of enterprise knowledge management?
- leetcode 1518. 换酒问题
- The 'name' attribute value associated with the element type 'item' cannot contain '& lt;' Character solution
- Basic use of JSON server
- Deployment之滚动更新策略。
- Minimally invasive electrophysiology has passed the registration: a listed enterprise with annual revenue of 190million minimally invasive mass production
- Uniapp 应用开机自启插件 Ba-Autoboot
- Siemens docking Leuze BPS_ 304i notes
猜你喜欢

奥浦迈生物通过注册:半年营收1.47亿 国寿成达与达晨是股东

FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be depreca

快速读入

GMT安装与使用

DIY system home page, your personalized needs PRO system to meet!

What SaaS architecture design does a software architect need to know?

leetcode:704二分查找

Initialization examples of several modes of mma8452q

The usage and Simulation Implementation of vector in STL

Library automatic reservation script
随机推荐
Developing NES games (cc65) 05 and palette with C language
【Base】优化性能到底在优化啥?
Most of the interfaces of Tiktok are already available, and more interfaces are still open. Please look forward to it
Not optimistic about Apple making AR, Luo Yonghao: I'll do it myself
图书馆自动预约脚本
IO流再回顾,深入理解序列化和反序列化
Baidu map API adds information window circularly. The window only opens at the last window position and the window information content is the same solution
C语言项目中使用json
[cute new problem solving] climb stairs
leetcode:数组
DART 三维辐射传输模型申请及下载
连通块&&食物链——(并查集小结)
Communication example between upper computer and Mitsubishi fn2x
STM32 loopback structure receives and processes serial port data
输入字符串,内有数字和非字符数组,例如A123x456将其中连续的数字作为一个整数,依次存放到一个数组中,如123放到a[0],456放到a[1],并输出a这些数
Deployment之滚动更新策略。
Marketing play is changeable, and understanding the rules is the key!
03 pyechars 直角坐标系图表(示例代码+效果图)
Science 重磅:AI设计蛋白质再获突破,可设计特定功能性蛋白质
奥浦迈生物通过注册:半年营收1.47亿 国寿成达与达晨是股东