Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SuperPrime_HW2.cpp #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 52 additions & 54 deletions SuperPrime_HW2.cpp
Original file line number Diff line number Diff line change
@@ -1,94 +1,92 @@
#include <iostream>
#include <vector>
#include <math.h>
class Nature {
private:
public:
int num;
int k=0,sum=0;
public:
Nature():num(0){
std::cout << "Default Create Nature as " << num << std::endl;
}
Nature(int n):num(n) {
std::cout << "Create Nature as " << num << std::endl;
}
Nature(const Nature &nat):num(nat.num){
std::cout << "Copy Create Nature as " << num << std::endl;
}
~Nature() {
std::cout << "Destroy Nature as " << num << std::endl;
}
bool isPrime() {
Nature(int n):num(n) {;}
Nature(const Nature &nat):num(nat.num){ ; }

~Nature() { ; }
bool IsPrime(int num) {
if(num == 1 || num == 0)
return false;
for(int i = 2; i <= (int)sqrt(num); i++)
for(int i=2;i<=(int)sqrt(num);i++)
{
if(num % i == 0)
return false;
}
return true;
}
bool isSuperPrime(int num) {
int a=num/100;
int b=(num/10)%10;
int c=num%10;
int n1=a+b+c,n2=a*b*c,n3=a*a+b*b+c*c;
if(IsPrime(num)&&IsPrime(n1)&&IsPrime(n2)&&IsPrime(n3))
{
return true;
}
return false;
}
int compare(const Nature &nat) {
if (num > nat.num)
return 1;
else if(num == nat.num)
return 0;

return -1;
}
private:
};
class SuperPrime : public Nature {
class SuperPrime {
private:
int num;
int k=0;
int sum=0;
std::vector<Nature> natures;
public:
SuperPrime(int n):num(n) {
}
bool isPrime() {
Nature nat(num);
return nat.isPrime();
}
int compare(const SuperPrime &nat) {
if (num > nat.num)
SuperPrime(int a, int b) {
for(int i = a; i< b; i++) {
Nature nat(i);//把数字转化为对象
if(nat.IsPrime(i))
natures.push_back(nat);
}
}
~SuperPrime() {;}
int compare(const SuperPrime &nat) {
if (num >nat.num)
return 1;
else if(num == nat.num)
return 0;

return -1;
}
};
class Container {
private:
std::vector<SuperPrime> natures;
public:
Container(int a, int b) {
std::cout << "Create SuperPrime from " << a << " to " << b << std::endl;
for(int i = a; i < b; i++) {
SuperPrime nat(i);
std::cout << "HAHA" << std::endl;
if(nat.isSuperPrime())
natures.push_back(nat);
std::cout << "DDDDD" << std::endl;
}
}
~Container() {
std::cout << "Destroy SuperPrime " << std::endl;
}

SuperPrime max() {
std::vector<SuperPrime>::iterate it = natures.begin();
Nature max() {
std::vector<Nature>::iterator it = natures.begin();
Nature max(0);
for(; it != natures.end(); it ++) {
if (max.compare(*it)) {
if(it->isSuperPrime(it->num))
{
if (max.compare(*it)) {
max = *it;
}
k++;
sum+=it->num;
}
}
return max;
}
void show()
{
std::cout<<"超级素数的个数为:"<<k<<std::endl;
std::cout<<"超级素数的和为:" <<sum<<std::endl;
}
};

int main() {
SuperPrime sp(10, 13);
Nature n = sp.max();
std::cout << "��󳬼�������" ;
n.show();

SuperPrime sp(100,1000);
Nature n=sp.max();
std :: cout << "最大的超级素数是" << n.num<< std :: endl;
sp.show();
return 0;
}