From 63d4434dce4e541c4a5df950442c1761fdc87721 Mon Sep 17 00:00:00 2001 From: Link-aka <1660306747@qq.com> Date: Sun, 25 Apr 2021 11:02:48 +0800 Subject: [PATCH] helloworld add commit --- SuperPrime_HW2.cpp | 194 ++++++++++++++++++++++++++------------------- 1 file changed, 111 insertions(+), 83 deletions(-) diff --git a/SuperPrime_HW2.cpp b/SuperPrime_HW2.cpp index ec1c77f3..b6fd7760 100644 --- a/SuperPrime_HW2.cpp +++ b/SuperPrime_HW2.cpp @@ -1,94 +1,122 @@ #include #include class Nature { -private: - int num; -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() { - if(num == 1 || num == 0) - return false; - for(int i = 2; i <= (int)sqrt(num); i++) - { - if(num % i == 0) - return false; - } - return true; - } - 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 { -private: - int num; public: - SuperPrime(int n):num(n) { - } - bool isPrime() { - Nature nat(num); - return nat.isPrime(); - } - int compare(const SuperPrime &nat) { - if (num > nat.num) - return 1; - else if(num == nat.num) - return 0; - - return -1; - } + int num; + + Nature() :num(0) { + // std::cout << "Default Create Nature as " << num << std::endl; + }// num = 0 + + Nature(int n) :num(n) { + // std::cout << "Create Nature as " << num << std::endl; + }//num = n + + 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 is_superPrime(int a) { + int hun, ten, one, sum, mul, square_sum; + hun = a / 100; + ten = (a - 100 * hun) / 10; + one = a - 100 * hun - 10 * ten; + mul = hun * ten * one; + sum = hun + ten + one; + square_sum = hun * hun + ten * ten + one * one; + if (prime(a) && prime(sum) && prime(mul) && prime(square_sum)) + { + return 1; + } + else return 0; + + } + int prime(int a) { + int i, b = 0; + int c = 0; + if (a == 2) + b = 1; + + for (i = 2; i < a; i++) { + if (a % i == 0) + { + c++; + } + } + if (c == 0) { + b = 1; + } + + return b; + + }//ÅжÏÒ»¸öÊý±¾ÉíÊDz»ÊÇËØÊý £¬ÊÇΪ1£¬²»ÊÇΪ0 + }; -class Container { + + + +class SuperPrime { private: - std::vector natures; + std::vector 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; + SuperPrime(int a, int b) { + std::cout << "Create SuperPrime from " << a << " to " << b << std::endl; + for (int i = a; i < b; i++) { + Nature nat(i); + // std::cout << "HAHA" << std::endl; + if (nat.is_superPrime(i)){ + natures.push_back(nat); + } + + // std::cout << "DDDDD" << std::endl; + } + } + + ~SuperPrime() { + // std::cout << "Destroy SuperPrime " << std::endl; + } + + + + Nature max() { + std::vector::iterator it = natures.begin(); + Nature max(0); + for (; it != natures.end(); it++) { + if ((*it).is_superPrime(it->num)) { + + max = *it; + + } + } + return max; } - } - ~Container() { - std::cout << "Destroy SuperPrime " << std::endl; - } - - SuperPrime max() { - std::vector::iterate it = natures.begin(); - Nature max(0); - for(; it != natures.end(); it ++) { - if (max.compare(*it)) { - max = *it; - } + void show() { + std::vector::iterator it = natures.begin(); + int sum1 = 0; + int sum2 = 0; + for (; it != natures.end(); it++) { + if ((*it).is_superPrime(it->num)) { + sum1 += (*it).num; + sum2++; + //std::cout << std::endl; + //std::cout << (*it).num; + } + } + std::cout << "³¬¼¶ËØÊý¸öÊý£º" << sum2 << std::endl; + std::cout << "³¬¼¶ËØÊýµÄºÍ£º" << sum1 << std::endl; } - return max; - } + + }; int main() { - SuperPrime sp(10, 13); - Nature n = sp.max(); - std::cout << "��󳬼�������" ; - n.show(); - - return 0; -} + SuperPrime sp(100, 999); + Nature n = sp.max(); + sp.show(); + std::cout << "×î´óµÄ³¬¼¶ËØÊý£º" << n.num; + + return 0; +} +