-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSapXepPhanTuNguyenTo.cpp
82 lines (65 loc) · 1.36 KB
/
SapXepPhanTuNguyenTo.cpp
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
#include <iostream>
using namespace std;
void nhapmang(int arr[], int n)
{
for ( int i = 0; i < n; i++)
cin >> arr[i];
}
void xuatmang(int arr[], int n)
{
for ( int i = 0; i < n; i++)
cout << arr[i] << " ";
}
void sapxep(int a[], int n)
{
int temp = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
if ( a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
bool isPrime(int n)
{
if (n <= 1)
return false;
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
int main()
{
int n, count_prime, j;
count_prime = 0;
j = 0;
cin >> n;
int arr[n];
nhapmang(arr, n);
for ( int i = 0; i < n; i++)
if ( isPrime(arr[i]) == 1)
count_prime++;
int arr1[count_prime];
for ( int i = 0; i < n; i++)
if ( isPrime(arr[i]) == 1)
{
arr1[j] = arr[i];
j++;
}
int bienmoi = 0 ;
sapxep(arr1, count_prime);
for ( int i = 0; i < n; i++)
if ( isPrime(arr[i]) == 1)
{
arr[i] = arr1[bienmoi++];
}
xuatmang(arr, n);
return 0;
}