b680

輸入說明 :

第一個數字N代表共有幾位選手,N<= 200 ,且N必為8的倍數。 每列有兩項資料,用空白隔開1 10.80,1代表選手,80是他的最佳成績。假設每一位選手的最佳成績不重複。

輸出說明 :

每列共有資料兩個部分,第一個資料代表分組,後面8筆資料代表選手所排的賽道順序。如:1 15 10 2 6 11 5 13 16,最前面的1代表第一組,15 10 2 6 11 5 13 16都是選手編號,順序就是賽道的順序,最左邊為第1道,最右邊為第8道。

這題先定義一個選手資料的結構,把資料按照成績快到慢排序,

然後因為題目說一定會剛好是8的倍數, 以第1組會是(頭到尾), 第2組是(尾到頭),第3組會是(頭到尾),第4組是(尾到頭).....

一直到N/8 那麼多組,每組有8個人,所以我寫了1個for迴圈,裡面再分成一個(頭到尾)的迴圈,另一個是(尾到頭)的迴圈,把排序完的資料放進去,

最後題目講的不清楚,他要的跑到順序是7,5,3,1,2,4,6,8 按這個順序印出來

#include<stdio.h>
#include<stdlib.h>

struct Race
{    
    int num;
    double grade;
};

Race array[200];

int  cmpGrade(const void *a,const void *b)
{
    Race *pa = (Race *)a;
    Race *pb = (Race *)b;    
    return (pa->grade > pb->grade);
}

int main()
{
    int N;    //幾位選手 
    int i,j,road;

    scanf("%d",&N);
    road = N/8; //要分幾組 

    Race divide[road][8];

    for(i=0;i<N;i++)
        scanf("%d%lf",&array[i].num,&array[i].grade);

    qsort((void *)array,N,sizeof(Race),cmpGrade);


    int where=0;
    for(i=0;i<8;i+=2)
    {   

        for(j=0;j<road;j++)
        {    
            divide[j][i]=array[where];
            where++;
        }

        for(j=road-1;j>=0;j--)
        {   
            divide[j][i+1]=array[where];
            where++;    
        }
    }    

    int index2[] = {7,5,3,1,2,4,6,8};
    for(i=0;i<road;i++)
    {    
        printf("%d ",i+1);
        for(j=0;j<8;j++)
        {
            int temp2 = index2[j]-1;
            printf("%d ",divide[i][temp2].num);
        }    
        printf("\n");
    }
    return 0;    
}