日常课后巩固ing,不过KNN可能是最近学的算法中最好实现的了(也可能我学的比较浅吧)。
总结:
没啥难点,除了理解算法流程外就只需要再了解下距离公式就行了,这里我为了省事直接选了欧式距离。
运行样例:
代码:
#include <bits/stdc++.h>
using namespace std;
/*测试用样本数据
1.5 1
1.92 3
1.7 2
1.73 2
1.6 1
1.75 2
1.5 1
1.6 1
2.05 3
1.9 3
1.68 2
1.78 2
1.70 2
1.68 2
1.65 2
测试用待处理数据
1.5
结果
1
*/
struct Node{
vector<double> v;
int re;
}board[10005];
struct cmp{
bool operator()(const pair<double,int> a,const pair<double,int> b){
return a.first < b.first;
}
};
priority_queue<pair<double,int>,vector&l