-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime_1324.cpp
More file actions
69 lines (61 loc) · 1.15 KB
/
Copy pathprime_1324.cpp
File metadata and controls
69 lines (61 loc) · 1.15 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
/*
* 1324: 素数问题(一)
* http://oj.ryipedu.com/problem.php?id=1324
*
* 给定 n 个(n<1000)小于 1000 的正整数,把其中的质数按升序输出。
*
*/
#include <iostream>
#include <math.h>
using namespace std;
bool is_prime(int x)
{
int val = sqrt(x);
for (int i = 2; i <= val; i++)
if (!(x % i))
return false;
return true;
}
bool is_contains(int *arr, int arr_len, int x)
{
for (int i = 0; i < arr_len; i++)
{
if (arr[i] == x)
{
return true;
}
}
return false;
}
int main()
{
int n, m, a[1000], ai = 0;
cin >> n;
// find all prime less than n
for (int i = 0; i < n; i++)
{
cin >> m;
if (is_prime(m) && !is_contains(a, ai, m))
{
a[ai++] = m;
}
}
for (int i = 0; i < ai; i++)
{
for (int j = 0; j < ai; j++)
{
if(a[i] < a[j])
{
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
for (int i = 0; i < ai; i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}