50107. Height and Weight
難度:2.8/5
記得存long long跟cmp回傳比較結果而不是相減。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
typedef struct{
char n[64];
long long h;
long long w;
} student;
int cmp(const void* s_a, const void* s_b){
student* a = (student*)s_a;
student* b = (student*)s_b;
if(a->w * b->h * b->h != b->w * a->h * a->h){
return a->w * b->h * b->h > b->w * a->h * a->h;
}
else if(a->w != b->w){
return a->w > b->w;
}
else if(a->h != b->h){
return a->h > b->h;
}
else{
return strcmp(a->n, b->n);
}
}
int n;
student arr[100005];
int main(){
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%s %lld %lld", arr[i].n, &arr[i].h, &arr[i].w);
}
qsort(arr, n, sizeof(student), cmp);
for(int i = 0; i < n; i++){
printf("%s %lld %lld\n", arr[i].n, arr[i].h, arr[i].w);
}
}