-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMisiRahasia2.c
More file actions
134 lines (121 loc) · 3.76 KB
/
Copy pathMisiRahasia2.c
File metadata and controls
134 lines (121 loc) · 3.76 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdio.h>
#include <stdlib.h>
typedef struct MisiRahasia2
{
int id;
int success;
int value;
} nimons;
void sortbubble(nimons arr[], int n){
if(n > 1){
int pass = 1 ;
int tukar = 1 ;
while(pass <= n-1 && tukar == 1){
tukar = 0 ;
for(int i = n-1 ; i > pass-1 ; i--){
if(arr[i].success < arr[i-1].success){
int temp = arr[i].success;
arr[i].success = arr[i-1].success;
arr[i-1].success = temp;
tukar = 1 ;
}
else if(arr[i].success == arr[i-1].success){
int temp;
if(arr[i].value < arr[i-1].value)
temp = arr[i].value;
arr[i].value = arr[i-1].value;
arr[i-1].value = temp;
tukar = 1 ;
if(arr[i].value == arr[i-1].value){
if(arr[i].id < arr[i-1].id)
temp = arr[i].id;
arr[i].id = arr[i-1].id;
arr[i-1].id = temp;
tukar = 1 ;
}
}
}
pass++ ;
}
}
}
void sortselectionSuccess(nimons arr[], int n){
if(n > 1){
for(int i = 0 ; i < n-1 ; i++){
int Imin = i ;
// Mencari minimum pada array [j..n-1]
for(int j = i+1 ; j < n ; j++){
if(arr[Imin].success > arr[j].success ){
Imin = j ;
}
// berdasarkan value
}
// Tukar arr[i] dengan arr[Imin]
nimons temp = arr[Imin] ;
arr[Imin] = arr[i] ;
arr[i] = temp ;
}
}
}
void sortselectionValue(nimons arr[], int n){
if(n > 1){
for(int i = 0 ; i < n-1 ; i++){
int Imin = i ;
// Mencari minimum pada array [j..n-1]
for(int j = i+1 ; j < n ; j++){
if(arr[Imin].value > arr[j].value && (arr[Imin].success == arr[j].success)){
Imin = j ;
}
// berdasarkan value
}
// Tukar arr[i] dengan arr[Imin]
nimons temp = arr[Imin] ;
arr[Imin] = arr[i] ;
arr[i] = temp ;
}
}
}
void sortselectionId(nimons arr[], int n){
if(n > 1){
for(int i = 0 ; i < n-1 ; i++){
int Imin = i ;
// Mencari minimum pada array [j..n-1]
for(int j = i+1 ; j < n ; j++){
if(arr[Imin].id > arr[j].id && (arr[Imin].value == arr[j].value)){
Imin = j ;
}
// berdasarkan value
}
// Tukar arr[i] dengan arr[Imin]
nimons temp = arr[Imin] ;
arr[Imin] = arr[i] ;
arr[i] = temp ;
}
}
}
int main(){
//init();
FILE *f = fopen("input.txt", "r");
nimons n;
nimons arr2[1002];
fscanf(f, "%d %d %d", &n.id, &n.success, &n.value);
int idx = 0;
while(n.id != -1){
arr2[idx] = n;
idx++;
fscanf(f, "%d %d %d", &n.id, &n.success, &n.value);
}
sortselectionSuccess(arr2, idx);
sortselectionValue(arr2, idx);
sortselectionId(arr2, idx);
FILE *out = fopen("query.txt", "r");
for(int i = 0; i < idx; i++){
printf("%d %d\n", arr2[i].id, arr2[i].value);
}
int m;
fscanf(out, "%d", &m);
fscanf(out, "%d", &m);
while(fscanf(out, "%d", &m) == 1){
printf("%d %d\n", arr2[m].id, arr2[m].value);
}
}