按要求,给国家进行排名。
4 4 4 8 1 6 6 2 4 8 2 2 12 4 0 1 2 3 4 2 8 10 1 8 11 2 8 12 3 8 13 4 0 3
1:3 1:1 2:1 1:2 1:1 1:1
这题要注意的是排名是在那M个国家之间的排名,与其他国家无关。另外每组数据后要输出一个空行,不然会 Presentation Error。
源代码:
#include <iostream>
using namespace std;
int main(){
int N, M;
while(cin >> N >> M){
int *goldMedal = new int[N];
int *medal = new int[N];
int *population = new int[N];
double *goldMedalRate = new double[N];
double *medalRate = new double[N];
int *country = new int[M];
//输入条件,并计算比例
for(int i=0; i<N; i++){
cin >> goldMedal[i];
cin >> medal[i];
cin >> population[i];
goldMedalRate[i] = (double) goldMedal[i] / (double) population[i];
medalRate[i] = (double) medal[i] / (double) population[i];
}
for(int i=0; i<M; i++){
cin >> country[i];
}
//计算排名
for(int i=0; i<M; i++){
int bestRanking = M + 1;
int rankingMethod = 0;
int ranking[4] = {1, 1, 1, 1};
for(int j=0; j<M; j++){
if(goldMedal[country[j]] > goldMedal[country[i]]) ranking[0] ++;
if(medal[country[j]] > medal[country[i]]) ranking[1] ++;
if(goldMedalRate[country[j]] > goldMedalRate[country[i]]) ranking[2] ++;
if(medalRate[country[j]] > medalRate[country[i]]) ranking[3] ++;
}
for(int k=0; k<4; k++){
if(bestRanking > ranking[k]){
bestRanking = ranking[k];
rankingMethod = k + 1;
}
}
cout << bestRanking << ‘:‘ << rankingMethod << endl;
}
cout << endl;
}
return 0;
}
原文:http://www.cnblogs.com/chaos---/p/6517489.html