-
Notifications
You must be signed in to change notification settings - Fork 378
/
SuperPrime_HW3.cpp
58 lines (57 loc) · 1.07 KB
/
SuperPrime_HW3.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
#include <iostream>
#include <vector>
class Nature {
protected:
int num;
public:
Nature(int n) {
num = n;
}
virtual bool isPrime() {
for(int i = 2; i < num;i++) {
if(num % i == 0)
return false;
}
return true;
}
void show() {
std::cout << num << std::endl;
}
};
class SuperPrime:public Nature {
private:
public:
SuperPrime(int n):Nature(n) {
}
bool isPrime() {
int tmp = num + 1;
int sum = 0, mul = 1, sqr = 0;
while(tmp != 0) {
tmp = tmp / 10;
int x = tmp % 10;
sum += x;
mul *= x;
sqr += x*x;
}
Nature nat(sum), nat1(mul), nat2(sqr);
return Nature::isPrime() && nat.isPrime() && nat1.isPrime()&& nat2.isPrime();
}
};
int main() {
//create some objects
std::vector<Nature*> sps;
for (int i = 100; i < 999; i++) {
Nature *nat = new SuperPrime(i);
if (nat->isPrime())
sps.push_back(nat);
}
std::vector<Nature*>::iterator it;
for(it=sps.begin(); it!=sps.end();it++) {
(*it)->show();
}
for(it=sps.begin(); it!=sps.end();it++) {
delete *it;
}
//×î´óµÄ³¬¼¶ËØÊý
return 0;
}