1. 程式人生 > 其它 >【技術部落格】Spark效能優化指南——基礎篇

【技術部落格】Spark效能優化指南——基礎篇

#include<cstdio>
struct node
{
    int id;
    double tall,dif;//身高和相異度 
    int type;//型別 假定矮個為0,中等為1,高個為2 
}a[105],e[105];
int cnt[10],maxcnt,typ=-1;
int n,k;
double tabs(double x)
{
    return x<0?-x:x;
}
void sort(int x)//每次對鄰居集合排序,使得相異度最大的資料在棧頂 
{
    for(int i=x;i>1;i--)
    {
        if(e[i].dif<e[i-1
].dif) { node t=e[i]; e[i]=e[i-1]; e[i-1]=t; } else break; } return; } int main() { scanf("%d%d",&n,&k); scanf("%lf",&a[0].tall);//輸入待分類的資料 for(int i=1;i<=n;i++)//輸入已有的資料並計算相異度 { scanf("%lf %d",&a[i].tall,&a[i].type); a[i].id
=i; a[i].dif=tabs(a[i].tall-a[0].tall); } for(int i=1;i<=k;i++)//形成初始鄰居集合 { e[i]=a[i]; sort(i); } for(int i=k+1;i<=n;i++) { if(a[i].dif<e[k].dif)//新資料相異度更小,加入鄰居集合並重新排序 { e[k]=a[i]; sort(k); } }
for(int i=1;i<=k;i++) { printf("%d %.2lf %.2lf %d\n",e[i].id,e[i].tall,e[i].dif,e[i].type);//輸出最終鄰居集合內的資料 cnt[e[i].type]++; if(cnt[e[i].type]>maxcnt)//統計各個型別的數量 { maxcnt=cnt[e[i].type]; typ=e[i].type; } } printf("%d",typ);//輸出最終的分類 return 0; } /* 樣例資料: 15 5 1.62 1.60 0 2.00 2 1.90 1 1.88 1 1.70 0 1.85 1 1.59 0 1.70 0 2.20 2 2.10 2 1.80 1 1.95 1 1.90 1 1.80 1 1.75 1 */