-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmallLargeTriangles.c
More file actions
59 lines (52 loc) · 1.06 KB
/
smallLargeTriangles.c
File metadata and controls
59 lines (52 loc) · 1.06 KB
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;
float calculateArea(triangle tr){
float p, area;
p = (tr.a + tr.b + tr.c) / 2.0;
area = pow((p * (p-tr.a) * (p-tr.b) * (p-tr.c)), 0.5);
return area;
}
void swap(triangle* tr, triangle* fr)
{
triangle temp;
temp = *tr;
*tr = *fr;
*fr = temp;
}
void sort_by_area(triangle* tr, int n) {
int i, j;
int swapped;
for (i = 0; i < n - 1; i++) {
swapped = 0;
for (j = 0; j < n - i - 1; j++) {
if (calculateArea(tr[j]) > calculateArea(tr[j + 1])) {
swap(&tr[j], &tr[j + 1]);
swapped = 1;
}
}
if (swapped == 0)
break;
}
}
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;
}