打入预选赛十强赛赋予40,预选赛小组未出线的赋予50。对于亚洲杯,前四名取其排名,八强赋予5,十六强赋予9,预选赛没出线的赋予17。 首先需要做数据预处理: 归一化(Normalization):是为了将数据映射到0~1之间,去掉量纲的过程,让计算更加合理,不会因为量纲问题导致1米与100mm产生不同。 有了数据后,就可以开始着手写代码了:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
typedef struct Teams{
double X;
double Y;
double Z;
}team,*Team;
void iniTeams(team& t,double x,double y,double z){
t.X=x;
t.Y=y;
t.Z=z;
}
void printfTeams(Team t){
printf("其X值为%f\n",t->X);
printf("其Y值为%f\n",t->Y);
printf("其Z值为%f\n",t->Z);
}
double ED(Team team1,Team team2){
return sqrt(pow((team1->X-team2->X),2)+pow((team1->Y-team2->Y),2)+pow((team1->Z-team2->Z),2));
}
int belongs(team t,team teams[]){
double d[3]={0};
double min=65535;
int position=0;
for(int i=0;i<3;i++){
d[i]=ED(&t,&teams[i]);
if(d[i]<min){
min=d[i];
position=i;
}
}
return position;
}
team average(team a[],int numlen){
team t;
double averageX=0;
double averageY=0;
double averageZ=0;
for(int i=0;i<numlen;i++){
averageX+=(a[i].X/(double)numlen);
averageY+=(a[i].Y/(double)numlen);
averageZ+=(a[i].Z/(double)numlen);
}
t.X=averageX;t.Y=averageY;t.Z=averageZ;
return t;
}
team* CaclulateC(team teams[],int a[],int count1,int b[],int count2,int c[],int count3){
team *newC=(team*)malloc(sizeof(team)*3);
team A[count1]={0}; team B[count2]={0}; team C[count3]={0};
for(int i=0;i<count1;i++){
A[i]=teams[a[i]];
}
newC[0]=average(A,count1);
for(int i=0;i<count2;i++){
B[i]=teams[b[i]];
}
newC[1]=average(B,count2);
for(int i=0;i<count3;i++){
C[i]=teams[c[i]];
}
newC[2]=average(C,count3);
return newC;
}
bool teamequal(team t1,team t2){
if(t1.X==t2.X and t1.Y==t2.Y and t1.Z==t2.Z){
return true;
}
return false;
}
int main(){
const char* name[15]={"中国","日本","韩国","伊朗","沙特","伊拉克","卡塔尔","阿联酋","乌兹别克斯坦","泰国","越南","阿曼","巴林","朝鲜","印尼"};
double data[15][3]={
1,1,0.5,
0.3,0,0.19,
0,0.15,0.13,
0.24,0.76,0.25,
0.3,0.76,0.06,
1,1,0,
1,0.76,0.5,
1,0.76,0.5,
0.7,0.76,0.25,
1,1,0.5,
1,1,0.25,
1,1,0.5,
0.7,0.76,0.5,
0.7,0.68,1,
1,1,0.5
};
team teams[15]={0};
for(int i=0;i<15;i++){
iniTeams(teams[i],data[i][0],data[i][1],data[i][2]);
}
team C[3]={0};
iniTeams(C[0],0.3,0,0.19);iniTeams(C[1],0.7,0.76,0.5);iniTeams(C[2],1,1,0.5);
int Count1=0;int Count2=0;int Count3=0;
int *a=nullptr; int *b=nullptr; int *c=nullptr;
while(true){
a=(int*)malloc(sizeof(int)*15); b=(int*)malloc(sizeof(int)*15); c=(int*)malloc(sizeof(int)*15); int j=0,k=0,l=0;
int count1=0,count2=0,count3=0;
for(int i=0;i<15;i++){
if(belongs(teams[i],C)==0){
a[j++]=i;
count1++;
}
if(belongs(teams[i],C)==1){
b[k++]=i;
count2++;
}
if(belongs(teams[i],C)==2){
c[l++]=i;
count3++;
}
}
team* newC=CaclulateC(teams,a,count1,b,count2,c,count3);
Count1=count1;Count2=count2;Count3=count3;
if(teamequal(newC[0],C[0]) and teamequal(newC[1],C[1]) and teamequal(newC[2],C[2])){
break;
}
for(int i=0;i<3;i++){
C[i].X=newC[i].X;
C[i].Y=newC[i].Y;
C[i].Z=newC[i].Z;
}
free(a); free(b); free(c);
}
printf("经过聚类后:\n");
printf("亚洲一流队伍有%d支分别是 ",Count1);
for(int i=0;i<Count1;i++){
printf("%s ",name[a[i]]);
}
printf("\n");
printf("亚洲二流队伍有%d支分别是 ",Count2);
for(int i=0;i<Count2;i++){
printf("%s ",name[b[i]]);
}
printf("\n");
printf("亚洲三流队伍有%d支分别是 ",Count3);
for(int i=0;i<Count3;i++){
printf("%s ",name[c[i]]);
}
printf("\n");
return 0;
}
|