-
题目地址:http://ac.jobdu.com/problem.php?pid=1007
-
按要求,给国家进行排名。
题目描述:
-
输入:
-
有多组数据。
第一行给出国家数N,要求排名的国家数M,国家号从0到N-1。
第二行开始的N行给定国家或地区的奥运金牌数,奖牌数,人口数(百万)。
接下来一行给出M个国家号。
-
输出:
-
排序有4种方式: 金牌总数 奖牌总数 金牌人口比例 奖牌人口比例
对每个国家给出最佳排名排名方式 和 最终排名
格式为: 排名:排名方式
如果有相同的最终排名,则输出排名方式最小的那种排名,对于排名方式,金牌总数 < 奖牌总数 < 金牌人口比例 < 奖牌人口比例
如果有并列排名的情况,即如果出现金牌总数为 100,90,90,80.则排名为1,2,2,4.
每组数据后加一个空行。
-
样例输入:
-
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个国家,除此之外的所有国家都没关系。
一定要注意这个。
1、输入n个国家的数据,需要保存,因为后续会排序用到。
2、输入m个国家的国家号。注意从0开始。
3、对m个国家排序。按照要求序列排序。
这个排序思路是这样的,针对每个国家,最初排名都是1,然后和其他国家相比,如果落后,则排名+1。
那么这样一个国家的排名就出来了。循环四个排名,记录最小的名次和方式。
4、按照结果输出。
C++ AC
#include <stdio.h>
const int maxn = 100000;
int i , j;
int n;
int m;
struct Country{
int goldNum;
int medalNum;
double rate1;
double rate2;
}countrys[maxn];
int needArr[maxn];
int main(){
while(scanf("%d%d",&n,&m) != EOF){
for(i = 0; i < n ; i++){
int peopleNum ;
scanf("%d%d%d", &countrys[i].goldNum ,&countrys[i].medalNum, &peopleNum);
countrys[i].rate1 = (double)countrys[i].goldNum/peopleNum;
countrys[i].rate2 = (double)countrys[i].medalNum/peopleNum;
}
for(i = 0; i < m ; i++){
scanf("%d", &needArr[i]);
}
for(i = 0; i < m ; i++){
int rank[4] = {1,1,1,1};
for(j = 0; j < m ; j++){
if(needArr[j] == needArr[i]){
continue;
}
if (countrys[needArr[i]].goldNum < countrys[needArr[j]].goldNum) {
rank[0]++;
}
if (countrys[needArr[i]].medalNum < countrys[needArr[j]].medalNum) {
rank[1]++;
}
if (countrys[needArr[i]].rate1 < countrys[needArr[j]].rate1) {
rank[2]++;
}
if (countrys[needArr[i]].rate2 < countrys[needArr[j]].rate2) {
rank[3]++;
}
}
int high = n+1;
int seq = 0;
for (j = 0; j < 4; j++) {
if (rank[j] < high) {
high = rank[j];
seq = j;
}
}
printf("%d:%d\n",high,seq+1);
}
printf("\n");
}
return 0;
}
/**************************************************************
Problem: 1007
User: wangzhenqing
Language: C++
Result: Accepted
Time:0 ms
Memory:3756 kb
****************************************************************/
Java AC 其实可以改简单点,恩,我就不改了。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
/*
* 1007
*/
public static void main(String[] args) throws Exception{
StreamTokenizer st = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
while (st.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) st.nval;
st.nextToken();
int m = (int) st.nval;
Map<Integer, Double[]> numMap =
new HashMap<Integer, Double[]>();
List<Double[]> counList = new ArrayList<Double[]>();
for (int i = 0; i < n; i++) {
Double[]country = new Double[5];
country[0] = (double)i;
st.nextToken();
country[1] = (double)(int) st.nval;
st.nextToken();
country[2] = (double)(int) st.nval;
st.nextToken();
int allNum = (int) st.nval;
country[3] = country[1]/allNum;
country[4] = country[2]/allNum;
numMap.put(i, country);
counList.add(country);
}
List<Integer> needNumList = new ArrayList<Integer>();
for (int i = 0; i < m; i++) {
st.nextToken();
int needNum = (int)st.nval;
needNumList.add(needNum);
}
for (int i = 0; i < m; i++) {
int needNum = needNumList.get(i);
Double[]country = new Double[5];
country = numMap.get(needNum);
int rank[] = {1,1,1,1};
for (int j = 0; j < n; j++) {
Double []newCou = counList.get(j);
if (!needNumList.contains(
(int)(double)newCou[0])) {
continue;
}
if ((double)newCou[0] == (double)needNum) {
continue;
}
if (country[1] < newCou[1]) {
rank[0]++;
}
if (country[2] < newCou[2]) {
rank[1]++;
}
if (country[3] < newCou[3]) {
rank[2]++;
}
if (country[4] < newCou[4]) {
rank[3]++;
}
}
int high = n+1;
int seq = 0;
for (int j = 0; j < 4; j++) {
if (rank[j] < high) {
high = rank[j];
seq = j;
}
}
System.out.println(high+":"+(seq+1));
}
System.out.println();
}
}
}
/**************************************************************
Problem: 1007
User: wzqwsrf
Language: Java
Result: Accepted
Time:70 ms
Memory:14616 kb
****************************************************************/