-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathExample.c
More file actions
36 lines (31 loc) · 929 Bytes
/
Example.c
File metadata and controls
36 lines (31 loc) · 929 Bytes
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
// Cho mảng các phần tử số, hãy in ra các phần tử của mảng sao cho mỗi phần
// tử chỉ xuất hiện trên màn hình duy nhất một lần.
#include <stdio.h>
/*
Thuật toán:
- lặp từ đầu đến cuối mảng
- tại mỗi phần tử ta kiểm tra tất cả các phần tử trước nó xem
đã tồn tại hay chưa, nếu đã tồn tại thì bỏ qua, nếu chưa thì in ra
- khi kiểm tra dùng biến đánh dấu lại trạng thái tồn tại của giá trị đó
*/
void showArrayElements(int *arr, int n) {
int i, j;
int flag;
for (i = 0; i < n; i++) {
flag = 1;
for (j = 0; j < i; j++) {
if (arr[j] == arr[i]) {
flag = 0;
break;
}
}
if (flag == 1) {
printf("%5d", arr[i]);
}
}
}
int main() {
int arr[] = { 1, 2, 1, 4, 5, 2, 3, 6, 9, 8, 7, 5, 4, 2, 1, 3, 0 };
showArrayElements(arr, sizeof(arr) / (sizeof(int)));
return 0;
}