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
/*
* main.c
*
* Created on: 2013-1-18
* Author: GaoTong
*/
#include <stdio.h>
#include <stdlib.h>
//如果要优化的话,其实可用数组代替。 原理是一样的。
typedef struct Node {
int index, gold, total, people;
} Node, *pNode;
//本来打算用排序函数的,后来发现没有必要. 只对单个属性排序就行,没有必要排序这个对象。
//函数指针 数组. 函数名本身就是一个指针,执行函数的入口地址
//int (* funArr[4])(const void *, const void *);
int n, m;
pNode contrys; //存储国家信息
//定义要排序的国家
int * conIndexs;
int main() {
while (scanf("%d %d", &n, &m) != EOF) {
contrys = (pNode) malloc(n * sizeof(Node));
conIndexs = (int *) malloc(m * sizeof(int));
int i;
for (i = 0; i < n; i++) { //读取国家的信息
scanf("%d %d %d", &contrys[i].gold, &contrys[i].total,
&contrys[i].people);
contrys[i].index = i;
}
//读取需要排序的国家
for (i = 0; i < m; i++)
scanf("%d", &conIndexs[i]);
for (i = 0; i < m; i++) {
int lasttype = 1;
int lastcnt = n;
//依次用不同的排序方式
int type;
for (type = 1; type <= 4; type++) {
int cnt = 1;
int j;
//这里指比较需要 排名的国家,不是对所有国家的进行排名
for (j = 0; j < m; j++) {
//依次和其它国家比较, 如果其它国家比我大,排名就加1
if (i != j) {
if (type == 1) {
if (contrys[conIndexs[i]].gold < contrys[conIndexs[j]].gold)
cnt++;
}
else if (type == 2) {
if (contrys[conIndexs[i]].total < contrys[conIndexs[j]].total)
cnt++;
}
else if (type == 3) {
if (contrys[conIndexs[i]].gold * contrys[conIndexs[j]].people < contrys[conIndexs[j]].gold *contrys[conIndexs[i]].people)
cnt++;
}
else if (type == 4) {
if (contrys[conIndexs[i]].total * contrys[conIndexs[j]].people < contrys[conIndexs[j]].total *contrys[conIndexs[i]].people)
cnt++;
}
}
//printf("cnt: %d ",cnt);
}
//printf("\n");
if (lastcnt > cnt) {
lastcnt = cnt;
lasttype = type;
}
}
printf("%d:%d\n", lastcnt, lasttype);
}
printf("\n");
}
return 0;
}
不用结构体,还是原来的算法。
#include <stdio.h>
#include <stdlib.h>
int * gold, * total, * people;
int n, m;
//定义要排序的国家
int * conIndexs;
int main() {
while (scanf("%d %d", &n, &m) != EOF) {
gold = (int *) malloc(n * 4);
total = (int *) malloc(n * 4);
people = (int *) malloc(n * 4);
conIndexs = (int *) malloc(m * 4);
int i;
for (i = 0; i < n; i++) { //读取国家的信息
scanf("%d %d %d", &gold[i], &total[i], &people[i]);
}
//读取需要排序的国家
for (i = 0; i < m; i++)
scanf("%d", &conIndexs[i]);
for (i = 0; i < m; i++) {
int lasttype = 1;
int lastcnt = n;
//依次用不同的排序方式
int type;
for (type = 1; type <= 4; type++) {
int cnt = 1;
int j;
//这里指比较需要 排名的国家,不是对所有国家的进行排名
for (j = 0; j < m; j++) {
//依次和其它国家比较, 如果其它国家比我大,排名就加1
if (i != j) {
if (type == 1) {
if (gold[conIndexs[i]] < gold[conIndexs[j]])
cnt++;
} else if (type == 2) {
if (total[conIndexs[i]] < total[conIndexs[j]])
cnt++;
} else if (type == 3) {
if (gold[conIndexs[i]] * people[conIndexs[j]]
< gold[conIndexs[j]] * people[conIndexs[i]])
cnt++;
} else if (type == 4) {
if (total[conIndexs[i]] * people[conIndexs[j]]
< total[conIndexs[j]]
* people[conIndexs[i]])
cnt++;
}
}
}
if (lastcnt > cnt) {
lastcnt = cnt;
lasttype = type;
}
}
printf("%d:%d\n", lastcnt, lasttype);
}
printf("\n");
}
return 0;
}