鄭州輕工業大學 數據結構 拼題A -- 排序部分

鄭州輕工業大學 數據結構 拼題A – 排序部分

7-22 排序 (25分)

題目描述:
給定N個(長整型範圍內的)整數,要求輸出從小到大排序後的結果。
本題旨在測試各種不同的排序算法在各種數據情況下的表現。各組測試數據特點如下:
數據1:只有1個元素;
數據2:11個不相同的整數,測試基本正確性;
數據3:103個隨機整數;
數據4:104個隨機整數;
數據5:105個隨機整數;
數據6:105個順序整數;
數據7:105個逆序整數;
數據8:105個基本有序的整數;
數據9:105個隨機正整數,每個數字不超過1000。

輸入格式:
輸入第一行給出正整數N(≤10​5​​),隨後一行給出N個(長整型範圍內的)整數,其間以空格分隔。
輸出格式:
在一行中輸出從小到大排序後的結果,數字間以1個空格分隔,行末不得有多餘空格。

輸入樣例:

11
4 981 10 -17 0 -20 29 50 8 43 -5

輸出樣例:

-20 -17 -5 0 4 8 10 29 43 50 981

sort 完美水過~~

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXSIZE = 1e5 + 9;

long long a[MAXSIZE];
int n;

int main()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i ++)
        scanf("%lld", &a[i]);
        
    sort(a, a + n);
    
    for(int i = 0; i < n; i ++)
        printf("%lld%c", a[i], i == n-1 ? '\n': ' ');
    return 0;
}

7-23 互評成績 (25分)

題目描述:
學生互評作業的簡單規則是這樣定的:每個人的作業會被k個同學評審,得到k個成績。系統需要去掉一個最高分和一個最低分,將剩下的分數取平均,就得到這個學生的最後成績。本題就要求你編寫這個互評系統的算分模塊。

輸入格式:
輸入第一行給出3個正整數N(3 < N ≤10​4​​,學生總數)、k(3 ≤ k ≤ 10,每份作業的評審數)、M(≤ 20,需要輸出的學生數)。隨後N行,每行給出一份作業得到的k個評審成績(在區間[0, 100]內),其間以空格分隔。

輸出格式:
按非遞減順序輸出最後得分最高的M個成績,保留小數點後3位。分數間有1個空格,行首尾不得有多餘空格。

輸入樣例:

6 5 3
88 90 85 99 60
67 60 80 76 70
90 93 96 99 99
78 65 77 70 72
88 88 88 88 88
55 55 55 55 55

輸出樣例:

87.667 88.000 96.000

同樣的套用sort~~~

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXSIZE = 1e4 + 9;

int n, k, m;
int a[MAXSIZE][15];
double ans[MAXSIZE];

int main()
{
    scanf("%d%d%d", &n, &k, &m);
    
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < k; j++)
            scanf("%d", &a[i][j]);
            
        sort(a[i], a[i] + k);
        
        int sum = 0;
        for(int j = 1; j < k - 1; j++)
            sum += a[i][j];
            
        ans[i] = 1.0 * sum / (k - 2);
    }
    
    sort(ans, ans + n);
    
    for(int i = n - m; i < n; i++)
        printf("%.3f%c", ans[i], i == n - 1 ? '\n' : ' ');
    return 0;
}

7-24 人以羣分 (25分)

題目描述:
社交網絡中我們給每個人定義了一個“活躍度”,現希望根據這個指標把人羣分爲兩大類,即外向型(outgoing,即活躍度高的)和內向型(introverted,即活躍度低的)。要求兩類人羣的規模儘可能接近,而他們的總活躍度差距儘可能拉開。

輸入格式:
輸入第一行給出一個正整數N(2≤N≤10​5​​)。隨後一行給出N個正整數,分別是每個人的活躍度,其間以空格分隔。題目保證這些數字以及它們的和都不會超過2​31​​。

輸出格式:
按下列格式輸出:

Outgoing #: N1
Introverted #: N2
Diff = N3

其中N1是外向型人的個數;N2是內向型人的個數;N3是兩羣人總活躍度之差的絕對值。
輸入樣例1:

10
23 8 10 99 46 2333 46 1 666 555

輸出樣例1:

Outgoing #: 5
Introverted #: 5
Diff = 3611

輸入樣例2:

13
110 79 218 69 3721 100 29 135 2 6 13 5188 85

輸出樣例2:

Outgoing #: 7
Introverted #: 6
Diff = 9359

判斷總人數夠不夠均分(N% 2 == 0),能均分則對半分配,不能則判斷給哪邊多分一個能使兩羣人總活躍度之差的絕對值更大。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXSIZE = 1e5 + 9;

int n;
int a[MAXSIZE];

int main()
{
    scanf("%d", &n);
    
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
        
    sort(a, a + n);
    
    int mid = n / 2;
    
    int sum1 = 0;
    for(int i = 0; i < mid; i++)
        sum1 += a[i];
        
    int sum2 = 0;
    for(int i = mid; i < n; i++)
        sum2 += a[i];
        
    if(n % 2 == 0)
    {
        printf("Outgoing #: %d\n", mid);
        printf("Introverted #: %d\n", mid);
        printf("Diff = %d\n", abs(sum2 - sum1));
    }
    else
    {
        int sum3 = 0;
        for(int i = 0; i <= mid; i++)
            sum3 += a[i];
            
        int sum4 = 0;
        for(int i = mid + 1; i < n; i++)
            sum4 += a[i];
            
        if(abs(sum2 - sum1) > abs(sum4 - sum3))
        {
            printf("Outgoing #: %d\n", mid + 1);
            printf("Introverted #: %d\n", mid );
            printf("Diff = %d\n", abs(sum2 - sum1));
        }
        else
        {
            printf("Outgoing #: %d\n", mid );
            printf("Introverted #: %d\n", mid + 1);
            printf("Diff = %d\n", abs(sum4 - sum3));
        }
    }
    return 0;
}

7-25 尋找大富翁 (25分)

題目描述:
胡潤研究院的調查顯示,截至2017年底,中國個人資產超過1億元的高淨值人羣達15萬人。假設給出N個人的個人資產值,請快速找出資產排前M位的大富翁。

輸入格式:
輸入首先給出兩個正整數N(≤10​6​​)和M(≤10),其中N爲總人數,M爲需要找出的大富翁數;接下來一行給出N個人的個人資產值,以百萬元爲單位,爲不超過長整型範圍的整數。數字間以空格分隔。

輸出格式:
在一行內按非遞增順序輸出資產排前M位的大富翁的個人資產值。數字間以空格分隔,但結尾不得有多餘空格。

輸入樣例:

8 3
8 12 7 3 20 9 5 18

輸出樣例:

20 18 12

注意 n < m 的情況

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXSIZE = 1e6 + 9;

int n, m;
long long a[MAXSIZE];

bool cmp(long long x, long long y)
{
    return x > y;
}

int main()
{
    scanf("%d%d", &n, &m);
    
    for(int i = 0; i < n; i++)
        scanf("%lld", &a[i]);
        
    sort(a, a + n, cmp);
    
    if(n < m)
        m = n;
        
    for(int i = 0; i < m; i++)
        printf("%lld%c", a[i], i == m - 1 ? '\n' : ' ');
    return 0;
}

7-26 PAT排名彙總 (25分)

題目描述:
計算機程序設計能力考試(Programming Ability Test,簡稱PAT)旨在通過統一組織的在線考試及自動評測方法客觀地評判考生的算法設計與程序設計實現能力,科學的評價計算機程序設計人才,爲企業選拔人才提供參考標準(網址http://www.patest.cn)。
每次考試會在若干個不同的考點同時舉行,每個考點用局域網,產生本考點的成績。考試結束後,各個考點的成績將即刻彙總成一張總的排名表。
現在就請你寫一個程序自動歸併各個考點的成績並生成總排名表。

輸入格式:
輸入的第一行給出一個正整數N(≤100),代表考點總數。隨後給出N個考點的成績,格式爲:首先一行給出正整數K(≤300),代表該考點的考生總數;隨後K行,每行給出1個考生的信息,包括考號(由13位整數字組成)和得分(爲[0,100]區間內的整數),中間用空格分隔。

輸出格式:
首先在第一行裏輸出考生總數。隨後輸出彙總的排名表,每個考生的信息佔一行,順序爲:考號、最終排名、考點編號、在該考點的排名。其中考點按輸入給出的順序從1到N編號。考生的輸出須按最終排名的非遞減順序輸出,獲得相同分數的考生應有相同名次,並按考號的遞增順序輸出。

輸入樣例:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

輸出樣例:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

注意考號不足13位的情況

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXSIZE = 30009;

int n, k;

struct Stu
{
    long long id; // 考號
    int score; // 成績
    int last_r; // 最終排名
    int test_id; // 考點編號
    int cur_r; // 在該考點的排名
};

Stu a[MAXSIZE];

bool cmp(Stu x, Stu y)
{
    if(x.score != y.score)
        return x.score > y.score;
    return x.id < y.id;
}

int main()
{
    scanf("%d", &n);
    
    int cur_id = 1;
    int before = 0;
    int sum = 0;
    
    while(n--)
    {
        scanf("%d", &k);
        
        sum += k;
        
        for(int i = before; i < before + k; i++)
        {
            scanf("%lld %d", &a[i].id, &a[i].score);
            a[i].test_id = cur_id;
        }
        
        cur_id ++;
        
        sort(a + before, a + k + before, cmp);
        
        for(int i = before; i < before + k; i++)
        {
            if (i == before)
                a[i].cur_r = i + 1 - before;
            else
            {
                if(a[i].score == a[i - 1].score)
                    a[i].cur_r = a[i - 1].cur_r;
                else
                    a[i].cur_r = i + 1 - before;
            }
        }
        before += k;
    }
    
    sort(a, a + sum, cmp);
    
    for(int i = 0; i < sum; i++)
    {
        if (i == 0)
            a[i].last_r = i + 1;
        else
        {
            if(a[i].score == a[i - 1].score)
                a[i].last_r = a[i - 1].last_r;
            else
                a[i].last_r = i + 1;
        }
    }
    
    printf("%d\n", sum);
    
    for(int i = 0; i < sum; i ++)
        printf("%013lld %d %d %d\n", a[i].id, a[i].last_r, a[i].test_id, a[i].cur_r);
    return 0;
}

總結:sort太好用辣hhhhh

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章