KNN算法C++实现。C++11
/* ***********************************************
Author :guanjun
Created Time :2016/6/20 18:22:32
File Name :1.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <fstream>
#define maxn 10010
using namespace std;
int k;
struct node{
double x1,x2,x3,x4,x5;
string s;
}nod[maxn];
pair<double,string>p[maxn];
map<string,int>mp;
ifstream fin;
int input(string s){
fin.open(s);
if(!fin){
cout<<"can not open the file "<<s<<endl;
exit(1);
}
int i=1;
while(fin>>nod[i].x1>>nod[i].x2>>nod[i].x3>>nod[i].x4>>nod[i].x5>>nod[i].s){
nod[i].x1*=100.0;nod[i].x2*=100.0;nod[i].x3*=100.0;nod[i].x4*=100.0;nod[i].x5*=100.0;
i++;
}
return i;
}
double mul(double x,double y){
return (x-y)*(x-y);
}
double dis(node a,node b){
double tmp=mul(a.x1,b.x1)+mul(a.x2,b.x2)+mul(a.x3,b.x3)+mul(a.x4,b.x4)+mul(a.x5,b.x5);
return<