-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmall_triangles-large_triangles.c
59 lines (54 loc) · 1.19 KB
/
small_triangles-large_triangles.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
56
57
58
59
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct triangle
{
int a;
int b;
int c;
};
typedef struct triangle triangle;
void sort_by_area(triangle* tr, int n) {
/**
* Sort an array a of the length n
*/
int i, j;
double p, heron, temp;
double arr[n];
triangle tempTri;
for(i = 0; i < n; i++) {
p = (tr[i].a + tr[i].b + tr[i].c) / 2.0;
heron = sqrt(p * (p - tr[i].a) * (p - tr[i].b) * (p - tr[i].c));
arr[i] = heron;
//printf("area: %lf\n", arr[i]);
}
for(i = 0; i < n - 1; i++) {
for(j = 0; j < n - i - 1; j++) {
if(arr[j] > arr[j + 1]) {
tempTri = tr[j];
tr[j] = tr[j + 1];
tr[j + 1] = tempTri;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
/**
* Sort an array a of the length n
*/
}
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;
}