-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortbyweights.c
More file actions
68 lines (64 loc) · 1.42 KB
/
sortbyweights.c
File metadata and controls
68 lines (64 loc) · 1.42 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
60
61
62
63
64
65
66
67
68
Given a set of numbers like <10, 36, 54,89,12> we want to find sum of weights based on the following conditions
1. 5 if a perfect square
2. 4 if multiple of 4 and divisible by 6
3. 3 if even number
And sort the numbers based on the weight and print it as follows
<10,its_weight>,<36,its weight><89,its weight>
Should display the numbers based on increasing order.
Code:
#include<stdio.h>
struct node
{
int number;
int weight;
};
int isPerfect(int num)
{
int ind;
for(ind=1;ind<=num/2;ind++)
if(ind*ind==num)
return 1;
return 0;
}
void computeWeight(struct node *arr,int num)
{
int ind1;
for(ind1=0;ind1<num;ind1++)
{
if(isPerfect(arr[ind1].number))
arr[ind1].weight=arr[ind1].weight+5;
if(arr[ind1].number%4==0 && arr[ind1].number%6==0)
arr[ind1].weight=arr[ind1].weight+4;
if(arr[ind1].number%2==0)
arr[ind1].weight=arr[ind1].weight+3;
}
}
void insertionSort(struct node *arr,int num)
{
int ind1,ind2;
struct node temp;
for(ind1=1;ind1<num;ind1++)
{
ind2=ind1-1;
temp=arr[ind1];
while(ind2>=0 && arr[ind2].weight<temp.weight)
{
arr[ind2+1]=arr[ind2];
ind2=ind2-1;
}
arr[ind2+1]=temp;
}
}
int main()
{
int num,ind1;
scanf("%d",&num);
struct node arr[num];
for(ind1=0;ind1<num;arr[ind1].weight=0,ind1++)
scanf("%d",&arr[ind1].number);
computeWeight(arr,num);
insertionSort(arr,num);
for(ind1=0;ind1<num;ind1++)
printf("%d - %d\n",arr[ind1].number,arr[ind1].weight);
return 0;
}