题解 | #奥运排序问题#
奥运排序问题
https://www.nowcoder.com/practice/100a4376cafc439b86f5f8791fb461f3
测试用例有个人口数是0的,可能有点问题
#include <iostream> #include <algorithm> #include <cstring> #include <cmath> using namespace std; struct score { int id; // 国家id int gold; // 金牌总数 int medals; // 奖牌总数 double gold_to_people; // 金牌人口比例 double medals_to_people; // 奖牌人口比例 int rank_1; int rank_2; int rank_3; int rank_4; }; int main() { int i, j, a[10], n, m; cin >> n >> m; int gold[n], medals[n], population[n]; int yaopaide[m]; for (i = 0; i < n; i++) { cin >> gold[i] >> medals[i] >> population[i]; // cout << gold[i] << medals[i] << population[i] << endl; } for (i = 0; i < m; i++) { cin >> yaopaide[i]; } struct score chengji[m]; // 计算各种方式下,各个要排的国家的数据,注意不是最终排名,而是存的数值 for (i = 0; i < m; i++) { chengji[i].id = yaopaide[i]; chengji[i].gold = gold[yaopaide[i]]; chengji[i].medals = medals[yaopaide[i]]; chengji[i].gold_to_people = gold[yaopaide[i]] * 1.0 / population[yaopaide[i]]; chengji[i].medals_to_people = medals[yaopaide[i]] * 1.0 / population[yaopaide[i]]; } int rank[4][m]; // 计算各种方式下,各个要排的国家的排名 int zancun[m]; // 第一种排序方式 for (i = 0; i < m; i++) { zancun[i] = chengji[i].gold; // cout << zancun[i]; } sort(zancun, zancun + m, greater<int>()); // 降序排列金牌数 for (i = m - 1; i >= 2; i--) { if (zancun[i] == zancun[i - 1]) // 剔除重名次,变成-1 zancun[i] = -1; } for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { if (chengji[i].gold == zancun[j]) { chengji[i].rank_1 = j + 1; break; } } // cout << chengji[i].rank_1 << endl; } // 第二种排序方式 for (i = 0; i < m; i++) { zancun[i] = chengji[i].medals; // cout << zancun[i]; } sort(zancun, zancun + m, greater<int>()); // 降序排列金牌数 for (i = m - 1; i >= 2; i--) { if (zancun[i] == zancun[i - 1]) // 剔除重名次,变成-1 zancun[i] = -1; } for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { if (chengji[i].medals == zancun[j]) { chengji[i].rank_2 = j + 1; break; } } // cout << chengji[i].rank_2 << endl; } double temp[m]; // 第三张排序方式 for (i = 0; i < m; i++) { temp[i] = chengji[i].gold_to_people; // cout << zancun[i]; } sort(temp, temp + m, greater<int>()); // 降序排列金牌数 for (i = m - 1; i >= 2; i--) { if (temp[i] == temp[i - 1]) // 剔除重名次,变成-1 temp[i] = -1.0; } for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { if (chengji[i].gold_to_people == temp[j]) { chengji[i].rank_3 = j + 1; break; } } // cout << chengji[i].rank_3 << endl; } // 第四张排序方式 for (i = 0; i < m; i++) { temp[i] = chengji[i].medals_to_people; // cout << zancun[i]; } sort(temp, temp + m, greater<int>()); // 降序排列金牌数 for (i = m - 1; i >= 2; i--) { if (temp[i] == temp[i - 1]) // 剔除重名次,变成-1 temp[i] = -1.0; } for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { if (chengji[i].medals_to_people == temp[j]) { chengji[i].rank_4 = j + 1; break; } } // cout << chengji[i].rank_4 << endl; } int min, minsosrt; for (i = 0; i < m; i++) { min = chengji[i].rank_1; minsosrt = 1; if (min > chengji[i].rank_2) { min = chengji[i].rank_2; minsosrt = 2; } if (min > chengji[i].rank_3) { min = chengji[i].rank_3; minsosrt = 3; } if (min > chengji[i].rank_4) { min = chengji[i].rank_4; minsosrt = 4; } cout << min << ':' << minsosrt << endl; } return 0; }