-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
55 lines (49 loc) · 969 Bytes
/
test.c
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
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct triangle
{
int a;
int b;
int c;
};
typedef struct triangle triangle;
void swap(int *pa, int *pb){
int temp = *pa;
*pa = *pb;
*pb = temp;
}
void sort_by_area(triangle* tr, int n) {
/**
* Sort an array a of the length n
*/
int s;
int area[n];
for (int i = 0; i < n;++i) {
s = (tr[i].a + tr[i].b + tr[i].c) / 2;
area[i] = pow(s * (s - tr[i].a) * (s - tr[i].b) * (s - tr[i].c), 0.5);
}
for (int i = 0; i < n;++i) {
for (int j = 0; j < n - i;++j) {
if (area[j] > area[j+1])
{
swap(&area[j], &area[j + 1]);
swap(&tr[j], &tr[j + 1]);
}
}
}
}
int main()
{
int n;
scanf("%d", &n);
triangle *tr = malloc(n * sizeof(triangle));
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &tr[i].a, &tr[i].b, &tr[i].c);
}
sort_by_area(tr, n);
for (int i = 0; i < n; i++) {
printf("%d %d %d\n", tr[i].a, tr[i].b, tr[i].c);
}
return 0;
}