如何将素数放入向量中?

时间:2016-01-07 10:51:34

标签: c++ vector

我在下面写了一段代码,要求用户输入并检查它是否为素数。我现在想要建立这个,所以当用户输入一个数字时,我会计算这个数字的素数并显示。例如,如果用户输入10,我的程序将输出'有4个素数'。我的想法是我必须将每个素数存储到一个向量中,但我的问题是如何?

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

int main()
{
    vector <double> primeHolder
    int x, i, flag;
    cout << "Enter a positive integer ";
    cin >> x;

    for (i = 2; i <= x/2; i++)
    {
        if(x%i == 0)
        {
            flag = 1;
            break;
        }
    }

    if (flag == 0)
        cout << "This is a prime number";
    else
        cout << "This is not a prime number";
    return 0;
}

3 个答案:

答案 0 :(得分:4)

首先,定义isPrime()函数以使代码更具可读性是有意义的:

bool isPrime(int x)
{
    for(int i = 2; i <= x/2; i++)
    {
        if(x%i == 0)
        {
            return false;
        }
    }
    return true;
}

然后您可以通过以下方式编写main()

int main()
{
    int input;
    cout << "Enter a positive integer: ";
    cin >> input;

    // You deal with integers here, so you shouldn't use vector<double>.
    // As all numbers are positive, you could also use unsigned int.
    vector<int> primeHolder;
    for(int i = 2; i <= input; i++)
    {
        // Test all values that are not larger than the input value.
        if(isPrime(i))
        {
            // If the tested value is a prime, append it to the vector.
            primeHolder.push_back(i);
        }
    }

    cout << "There are " << primeHolder.size() << " primes:" << endl;
    for(size_t j = 0; j < primeHolder.size(); j++)
    {
        // Print every prime number that was stored in the vector.
        // You can access vector elements similar to an array,
        // but you can also use iterators.
        cout << primeHolder[j] << endl;
    }
    return 0;
}

此代码为您的示例输入提供以下输出:

  

输入正整数:10
  有4个素数:
  2
  3
  5
  7

注意:上面的代码效率很低。如果你想处理大输入,你应该寻找一个更智能的算法,例如Sieve of Eratosthenes,正如@theoden在评论中提到的那样。

如果您想详细了解vector课程模板的功能,请查看documentation。该文档还包含示例代码。

答案 1 :(得分:1)

在if语句中,如果您发现它是素数,只需将其添加到向量中即可。

小样本代码:

if(x is prime)
{
    primeHolder.push_back(x);
}

答案 2 :(得分:0)

因为向量包含素数,所以通过将当前数除以向量中的元素来确定下一个素数要好得多。

程序可以按以下方式查看

#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>

int main()
{
    while ( true )
    {
        std::cout << "Enter a non-negative number (0-exit): ";

        unsigned int n = 0;
        std::cin >> n;

        if ( !n ) break;

        std::vector<unsigned int> primes;

        if ( n >= 2 ) primes.push_back( 2 );

        for ( unsigned int i = 3; i <= n; i += 2 )
        {
            if ( std::all_of( primes.begin(), primes.end(), std::bind1st( std::modulus<unsigned int>(), i ) ) )
            {
                primes.push_back( i );
            }
        }

        if ( !primes.empty() )
        {
            std::cout << "There are the following prime numbers up to " << n << ": ";
            for ( unsigned int x : primes ) std::cout << x << ' ';
            std::cout << std::endl;
        }
        else
        {
            std::cout << "There are no prime numbers up to " << n << std::endl;
        }           
    }

    return 0;
}

如果要按顺序输入以下数字

10 20 30 40 50 60 70 80 90 100 0

然后程序输出看起来像

Enter a non-negative number (0-exit): 10
There are the following prime numbers up to 10: 2 3 5 7 
Enter a non-negative number (0-exit): 20
There are the following prime numbers up to 20: 2 3 5 7 11 13 17 19 
Enter a non-negative number (0-exit): 30
There are the following prime numbers up to 30: 2 3 5 7 11 13 17 19 23 29 
Enter a non-negative number (0-exit): 40
There are the following prime numbers up to 40: 2 3 5 7 11 13 17 19 23 29 31 37 
Enter a non-negative number (0-exit): 50
There are the following prime numbers up to 50: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 
Enter a non-negative number (0-exit): 60
There are the following prime numbers up to 60: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 
Enter a non-negative number (0-exit): 70
There are the following prime numbers up to 70: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 
Enter a non-negative number (0-exit): 80
There are the following prime numbers up to 80: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 
Enter a non-negative number (0-exit): 90
There are the following prime numbers up to 90: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 
Enter a non-negative number (0-exit): 100
There are the following prime numbers up to 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 
Enter a non-negative number (0-exit): 0