-
Notifications
You must be signed in to change notification settings - Fork 44
/
bai4.5.c
62 lines (55 loc) · 993 Bytes
/
bai4.5.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
60
61
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
void nhap(int *arr, int *n) {
*n = 0;
while(*n <= 0) {
printf("Nhap n > 0: ");
scanf("%d", n);
}
int i;
for(i = 0; i < *n; i++) {
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
}
int timHieuMin(int *arr, int n, int x) {
int hieu = abs(arr[0] - x);
int i;
for(i = 1; i < n; i++) {
if(abs(arr[i] - x) < hieu) {
hieu = abs(arr[i] - x);
}
}
return hieu; // hieu nho nhat,
}
bool chuaXH(int *b, int n, int v) {
int i;
for(i = 0; i < n; i++) {
if(b[i] == v) {
return false;
}
}
return true;
}
void hienQK(int *arr, int n, int x, int hieu) {
int i;
int b[100];
int count = 0;
for(i = 0; i < n; i++) {
if(abs(arr[i] - x) == hieu && chuaXH(b, count, arr[i])) {
printf("%10d", arr[i]);
b[count++] = arr[i];
}
}
}
int main() {
int n, arr[100];
int x;
nhap(arr, &n);
printf("Nhap x: ");
scanf("%d", &x);
int hieu = timHieuMin(arr, n, x);
hienQK(arr, n, x, hieu);
return 0;
}