7-20 奧運排行榜 (25分)(c++ STL)

Kokonuts發表於2020-11-15

我開始水題了(我好像一直都在水題)
這個部落格裡有幾份測試資料
https://blog.csdn.net/u012860063/article/details/41407809?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param

#include <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;
const int INF = 65535;
const int MAXNUM = 1e5 + 5;
typedef struct info
{
    int id;
    int Gold_medals;
    int Number_of_medals;
    int Population;
    //GP——Gold_medals/Population
    double GP;
    //NP——Number_of_medals/Population
    double NP;
}info;

info method[4][MAXNUM];

bool cmp1(info a, info b)
{
    if (a.Gold_medals == b.Gold_medals) return a.id < b.id;
    return a.Gold_medals > b.Gold_medals; 
}

bool cmp2(info a, info b)
{
    if (a.Number_of_medals == b.Number_of_medals) return a.id < b.id;
    return a.Number_of_medals > b.Number_of_medals; 
}
bool cmp3(info a, info b)
{
    return a.GP > b.GP;
}

bool cmp4(info a, info b)
{
    return a.NP > b.NP;
}
 
int main()
{
    //ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 4; j++)
            method[j][i].id = i;
        scanf("%d %d %d", &method[0][i].Gold_medals, &method[0][i].Number_of_medals, &method[0][i].Population);
        method[0][i].GP = 1.0 * method[0][i].Gold_medals / method[0][i].Population;
        method[0][i].NP = 1.0 * method[0][i].Number_of_medals / method[0][i].Population;

        for (int j = 1; j < 4; j++)
        {
            method[j][i].Gold_medals = method[0][i].Gold_medals;
            method[j][i].Number_of_medals = method[0][i].Number_of_medals;
            method[j][i].Population = method[0][i].Population;
            method[j][i].GP = 1.0 * method[j][i].Gold_medals / method[j][i].Population;
            method[j][i].NP = 1.0 * method[j][i].Number_of_medals / method[j][i].Population;
        }
    }
    
    sort(method[0], method[0] + n, cmp1);
    sort(method[1], method[1] + n, cmp2);
    sort(method[2], method[2] + n, cmp3);
    sort(method[3], method[3] + n, cmp4);
    
    if (m > n) m = n;
    for (int i = 0; i < m; i++)
    {
        if (i) cout << ' ';
        
        int country, mtd, ans1 = INF, ans2 = 0;
        cin >> country;
        for (mtd = 0; mtd < 4; mtd++)
        {
            for (int j = 0; j < n; j++)
            {
                if (method[mtd][j].id == country)
                {
                    //有兩個測試點在這
                    //排名相同,在查詢的時候 儘量往前排
                    if (mtd == 0)
                    {
                        while (j >= 1 && method[mtd][j].Gold_medals == method[mtd][j-1].Gold_medals) j--;
                    }
                    else if (mtd == 1)
                    {
                        while (j >= 1 && method[mtd][j].Number_of_medals == method[mtd][j-1].Number_of_medals) j--;
                    }
                    else if (mtd == 2)
                    {
                        while (j >= 1 && method[mtd][j].GP == method[mtd][j-1].GP) j--;
                    }
                    else if (mtd == 3)
                    {
                        while (j >= 1 && method[mtd][j].NP == method[mtd][j-1].NP) j--;
                    }
                    if (ans1 > j)
                    {
                        ans1 = j;
                        ans2 = mtd;
                    }
                    break;
                }
            }
        }
        printf("%d:%d", ans1 + 1, ans2 + 1);
    }
    system("pause");
    return 0;
}

相關文章