diff --git a/SuperPrime_HW.cpp b/SuperPrime_HW.cpp index 0e004b50..00d60655 100644 --- a/SuperPrime_HW.cpp +++ b/SuperPrime_HW.cpp @@ -1,5 +1,100 @@ #include +#include +class Nature +{ +private: + int num; -int main() { - return 0; +public: + Nature(int n) + { + num = n; + } + ~Nature() {} + + bool isPrime() + { + for (int i = 2; i * i <= num; i++) + { + if (num % i == 0) + return false; + } + return true; + } + bool isSuperPrime() + { + int xx = num, a, sum = 0, mul = 1, sqrsum = 0; + while (xx != 0) + { + a = xx % 10; + sum += a; + mul *= a; + sqrsum += (a * a); + xx /= 10; + } + Nature mul1(mul), sum1(sum), sqrsum1(sqrsum); + if (mul == 0) + return false; + else if (this->isPrime() && mul1.isPrime() && sum1.isPrime() && sqrsum1.isPrime()) + return true; + else + return false; + } + int show() + { + return num ; + } +}; + +class SuperPrime +{ +private: + std::vector range; + +public: + SuperPrime(int a, int b) + { + for (int i = a; i < b; i++) + { + Nature nat(i); + if (nat.isSuperPrime()) + range.push_back(nat); + } + } + int max() + { + int Max = 0; + for (std::vector::iterator it = range.begin(); it != range.end(); it++) + { + if (it->show() > (it + 1)->show()) + Max = it->show(); + else + Max = (it + 1)->show(); + } + return Max; + } + + int sum() + { + int Sum = 0; + for (std::vector::iterator it = range.begin(); it != range.end(); it++) + { + Sum += it->show(); + } + return Sum; + } + + int howmany() + { + return range.size(); + } +}; + +int main() +{ + SuperPrime sp(100, 999); + std::cout << "³¬¼¶ËØÊýµÄ¸öÊý£º" << sp.max() << std::endl; + std::cout << "ËùÓеij¬¼¶ËØÊýÖ®ºÍ£º" << sp.sum() << std::endl; + std::cout << "×î´óµÄ³¬¼¶ËØÊý£º" << sp.howmany() << std::endl; + return 0; } diff --git a/SuperPrime_HW2.cpp b/SuperPrime_HW2.cpp index ec1c77f3..a4a31b8e 100644 --- a/SuperPrime_HW2.cpp +++ b/SuperPrime_HW2.cpp @@ -1,9 +1,11 @@ #include #include -class Nature { +class Nature +{ private: - int num; + int num; public: +<<<<<<< HEAD:SuperPrime_HW2.cpp Nature():num(0){ std::cout << "Default Create Nature as " << num << std::endl; } @@ -80,15 +82,114 @@ class Container { if (max.compare(*it)) { max = *it; } +======= + /*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() + { + for (int i = 2; i * i <= num; i++) + { + if (num % i == 0) + return false; + } + return true; } - return max; - } + bool isSuperPrime() + { + int xx = num, a, sum = 0, mul = 1, sqrsum = 0; + while (xx != 0) + { + a = xx % 10; + sum += a; + mul *= a; + sqrsum += (a * a); + xx /= 10; + } + Nature mul1(mul), sum1(sum), sqrsum1(sqrsum); + if (mul == 0) + return false; + else if (this->isPrime() && mul1.isPrime() && sum1.isPrime() && sqrsum1.isPrime()) + return true; + return false; + } + int show() + { + return num ; + } + int compare(const Nature &nat) + { + if (num > nat.num) + return 1 ; + else if (num == nat.num) + return 0 ; + return -1 ; + } + }; -int main() { - SuperPrime sp(10, 13); - Nature n = sp.max(); - std::cout << "��󳬼�������" ; - n.show(); +class SuperPrime +{ +private: + std::vector natures; + int num_of_SP , sum_of_SP , maxn ; +public: + SuperPrime(int a, int b) + { + num_of_SP = 0 ; + for(int i = a; i < b; i++) + { + Nature nat(i); + if (nat.isSuperPrime()) + { + natures.push_back(nat); + num_of_SP ++ ; + std::cout << "num_of_SP ++" << std::endl; + sum_of_SP += nat.show() ; + std::cout << "sum_of_SP=" << sum_of_SP << std::endl; + maxn = nat.show() ; + } + + } + } + ~SuperPrime() + { + std::cout << "Destroy SuperPrime " << std::endl; + } - return 0; + int max() + { + return maxn ; + } + int number() + { + return num_of_SP ; + } + int sum() + { + return sum_of_SP ; +>>>>>>> d2ccf5ff31e58708a3d65150e06c0d259d503ab4:SuperPrime-HW2.cpp + } +}; +int main() +{ + SuperPrime sp(100, 999); + std::cout << "最大的超级素数:" << sp.max() << std::endl ; + std::cout << "超级素数的个数:" << sp.number() << std::endl ; + std::cout << "超级素数的和:" << sp.sum()<< std::endl ; + + + return 0; } diff --git a/SuperPrime_HW3.cpp b/SuperPrime_HW3.cpp new file mode 100644 index 00000000..0b0f4e3d --- /dev/null +++ b/SuperPrime_HW3.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +class Nature +{ +protected: + int num; +public: + Nature(int n) + { + num = n ; + } + virtual bool isPrime() + { + for(int i = 2; i <= sqrt(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 ; + int sum = 0 , mul = 1 , sqr = 0; + while(tmp != 0) + { + int x = tmp % 10 ; + if ( !x ) + return false ; + tmp = 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 sps ; + for (int i = 100 ; i < 999 ; i ++ ) + { + Nature *nat = new SuperPrime(i) ; + if (nat->isPrime()) + sps.push_back(nat) ; + } + std::vector::iterator it; + for(it = sps.begin(); it != sps.end() ; it ++) + { + (*it)->show() ; + } + for(it = sps.begin() ; it!=sps.end() ; it ++) + { + delete *it ; + } + + //×î´óµÄ³¬¼¶ËØÊý + return 0 ; +} diff --git a/main b/main new file mode 100755 index 00000000..204a7698 Binary files /dev/null and b/main differ diff --git a/sp b/sp new file mode 100755 index 00000000..89d743c7 Binary files /dev/null and b/sp differ diff --git "a/\345\210\244\346\226\255\350\266\205\347\272\247\347\264\240\346\225\260 \345\206\257\346\260\270\351\224\264" "b/\345\210\244\346\226\255\350\266\205\347\272\247\347\264\240\346\225\260 \345\206\257\346\260\270\351\224\264" new file mode 100644 index 00000000..5b58a591 --- /dev/null +++ "b/\345\210\244\346\226\255\350\266\205\347\272\247\347\264\240\346\225\260 \345\206\257\346\260\270\351\224\264" @@ -0,0 +1,41 @@ +#include +int prime[100]; +bool isprime(int n) ; + +int main() +{ + int n = 100 , cnt = 0 , sum = 0 ; + while ( n < 1000 ) + { + if ( isprime(n) ) + { + int a = 0 , b = 0 , c = 0 , m = n ; + c = m % 10 ; + b = ( m /= 10 ) % 10 ; + a = m / 10 ; + if ( isprime(a + b + c) && isprime(a * b * c) && isprime( a * a + b * b + c * c ) ) + { + prime[cnt ++] = n ; + sum += n ; + } + } + n ++ ; + } + std::cout << "超级素数的个数:" << cnt << std::endl ; + std::cout << "所有的超级素数之和:" << sum << std::endl ; + std::cout << "最大的超级素数:" << prime[cnt - 1] << std::endl ; + + return 0 ; +} + +bool isprime(int n) +{ + for ( int i = 2 ; i * i <= n ; i ++ ) + { + if ( n % i == 0 ) + { + return false ; + } + } + return true ; +} diff --git "a/\347\273\237\350\256\241\350\266\205\347\272\247\347\264\240\346\225\260\357\274\210\347\261\273\345\222\214\345\257\271\350\261\241\357\274\211.cpp" "b/\347\273\237\350\256\241\350\266\205\347\272\247\347\264\240\346\225\260\357\274\210\347\261\273\345\222\214\345\257\271\350\261\241\357\274\211.cpp" new file mode 100644 index 00000000..d76f5119 --- /dev/null +++ "b/\347\273\237\350\256\241\350\266\205\347\272\247\347\264\240\346\225\260\357\274\210\347\261\273\345\222\214\345\257\271\350\261\241\357\274\211.cpp" @@ -0,0 +1,111 @@ +#include +#include +class Nature +{ +private: + int num ; +public: + Nature(int n) + { + num = n ; + } + ~Nature(){} + + /*int add(Nature sp) + { + return num + sp.num ; + } + + bool compare(Nature sp) + { + if (num > sp.num) + return true ; + return false ; + }*/ + bool isPrime() + { + for ( int i = 2 ; i * i <= num ; i ++ ) + { + if ( num % i == 0 ) + return false ; + } + return true ; + } + bool isSuperPrime() + { + int xx = num , a , sum = 0 , mul = 1 , sqrsum = 0 ; + while ( xx != 0 ) + { + a = xx % 10 ; + sum += a ; + mul *= a ; + sqrsum += ( a * a ) ; + xx /= 10 ; + } + Nature mul1(mul) , sum1(sum) , sqrsum1(sqrsum) ; + if (mul == 0) + return false ; + else if (this->isPrime() && mul1.isPrime() && sum1.isPrime() && sqrsum1.isPrime()) + return true ; + else + return false ; + } + int show() + { + return num ; + } +}; + +class SuperPrime +{ +private: + std::vector range ; +public: + SuperPrime(int a , int b) + { + for ( int i = a ; i < b ; i ++ ) + { + Nature nat(i) ; + if (nat.isSuperPrime()) + range.push_back(nat) ; + } + } + int max() + { + int Max = 0 ; + for (std::vector::iterator it = range.begin() ; it != range.end() ; it ++ ) + { + if ( it->show() > (it + 1)->show() ) + Max = it->show() ; + else + Max = (it + 1)->show() ; + } + return Max ; + } + + int sum() + { + int Sum = 0 ; + for (std::vector::iterator it = range.begin() ; it != range.end() ; it ++ ) + { + Sum += it->show() ; + } + return Sum ; + } + + int howmany() + { + return range.size(); + } +}; + +int main() +{ + SuperPrime sp(100 , 999) ; + std::cout << "×î´óµÄ³¬¼¶ËØÊý£º" << sp.max() << std::endl ; + std::cout << "³¬¼¶ËØÊýµÄºÍ£º" << sp.sum() << std::endl ; + std::cout << "³¬¼¶ËØÊýµÄ¸öÊý£º" << sp.howmany() << std::endl ; + return 0 ; +} + +