E - 穩定排序(結構體)

來源:互聯網
上載者:User

標籤:onclick   長度   一個   clu   pac   als   input   table   nbsp   

大家都知道,快速排序是不穩定的排序方法。 
如果對於數組中出現的任意a[i],a[j](i<j),其中a[i]==a[j],在進行排序以後a[i]一定出現在a[j]之前,則認為該排序是穩定的。 

某高校招生辦得到一份成績列表,上面記錄了考生名字和考產生績。並且對其使用了某排序演算法按成績進行遞減排序。現在請你判斷一下該排序演算法是否正確,如果正確的話,則判斷該排序演算法是否為穩定的。 

Input本題目包含多組輸入,請處理到檔案結束。 
對於每組資料,第一行有一個正整數N(0<N<300),代表成績列表中的考生數目。 
接下來有N行,每一行有一個字串代表考生名字(長度不超過50,僅包含‘a‘~‘z‘),和一個整數代表考生分數(小於500)。其中名字和成績用一個空格隔開。 
再接下來又有N行,是上述列表經過某排序演算法以後產生的一個序列。格式同上。Output對於每組資料,如果演算法是正確並且穩定的,就在一行裡面輸出"Right"。如果演算法是正確的但不是穩定的,就在一行裡面輸出"Not Stable",並且在下面輸出正確穩定排序的列表,格式同輸入。如果該演算法是錯誤的,就在一行裡面輸出"Error",並且在下面輸出正確穩定排序的列表,格式同輸入。 

注意,本題目不考慮該排序演算法是錯誤的,但結果是正確的這樣的意外情況。Sample Input

3aa 10bb 10cc 20cc 20bb 10aa 103aa 10bb 10cc 20cc 20aa 10bb 103aa 10bb 10cc 20aa 10bb 10cc 20

Sample Output

Not Stablecc 20aa 10bb 10RightErrorcc 20aa 10bb 10

 

題解:先按照題目要求,寫出穩定排序,(注意這裡是如果分數相同則按照先出現的排在前面,而不是按照姓名的字典序進行排列),然後再進行比較即可。

AC代碼

 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5  6 struct nood 7 { 8     char name[55]; 9     int score;10     int data;11 } student[3300], solution[3300];12 13 bool cmp(nood a, nood b)14 {15     if(a.score == b.score)16     {17         return a.data < b.data;18     }19     else20     {21         return a.score > b.score;22     }23 }24 25 int main()26 {27     int n;28     bool flag1, flag2;29     while(~scanf("%d", &n))30     {31         flag1 = flag2 = true;32         for(int i = 0; i < n; i++)33         {34             scanf("%s %d", student[i].name, &student[i].score);35             student[i].data = i;36         }37         for(int i = 0; i < n; i++)38         {39             scanf("%s %d", solution[i].name, &solution[i].score);40         }41         sort(student, student+n, cmp);42         for(int i = 0; i < n; i++)43         {44             if(solution[i].score != student[i].score)45             {46                 flag1 = false;47                 break;48             }49         }50         for(int i = 0; i < n; i++)51         {52             int name_cmp = strcmp(solution[i].name, student[i].name);53             if(name_cmp)54             {55                 flag2 = false;56                 break;57             }58         }59         if(flag1 && flag2)60             printf("Right\n");61         else if(flag1 && !flag2)62         {63             printf("Not Stable\n");64             for(int i = 0; i < n; i++)65                 printf("%s %d\n", student[i].name, student[i].score);66         }67         else if(!flag1 && !flag2)68         {69             printf("Error\n");70             for(int i = 0; i < n; i++)71                 printf("%s %d\n", student[i].name, student[i].score);72         }73     }74 75     return 0;76 }
View Code

 

E - 穩定排序(結構體)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.