输入样例:
10
3 2 22 10 58 8 125
5 1 345 3 211 5 233 7 13 8 101
1 7 8800
2 1 1000 2 1000
2 4 250 10 320
6 5 11 9 22 8 33 7 44 10 55 4 2
1 3 8800
2 1 23 2 123
1 8 250
4 2 121 4 516 7 112 9 10
输出样例:
1 11.63
2 3.63
8 3.63
3 2.11
7 1.69
6 -1.67
9 -2.18
10 -3.26
5 -3.26
4 -12.32
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node {
int on;
int cnt;
double num;
};
int cmp(const void *a, const void *b)
{
if (((*(struct Node *)a).num < (*(struct Node *)b).num) ||
(((*(struct Node *)a).num == (*(struct Node *)b).num) &&
((*(struct Node *)a).cnt < (*(struct Node *)b).cnt)) ||
(((*(struct Node *)a).num == (*(struct Node *)b).num) &&
((*(struct Node *)a).cnt == (*(struct Node *)b).cnt) &&
((*(struct Node *)a).on > (*(struct Node *)b).on))) {
return 1;
}
else {
return -1;
}
}
int main()
{
int N, k, i, n, on;
double num;
scanf("%d", &N);
struct Node *s = (struct Node *)malloc (sizeof(struct Node) * (N+1));
memset(s, 0, sizeof(struct Node) * (N+1));
for(k = 1; k <= N; k ++) {
double sum = 0;
scanf("%d", &n);
for(i = 0; i < n; i ++) {
scanf("%d %lf", &on, &num);
sum += num;
s[on].cnt ++;
s[on].num += num/100;
}
s[k].on = k;
s[k].num -= sum/100;
}
qsort(s + 1, N, sizeof(s[0]), cmp);
for(i = 1; i <= N; i ++) {
printf("%d %.2f\n", s[i].on, s[i].num);
}
return 0;
}
如果对C语言的快排函数不熟悉的,可以点击以下链接:
qsort()函数基本使用方法