简体   繁体   English

计算向量中出现5的次数

[英]Count how many times 5 appears in the vector

I have finished my program, and it runs perfectly fine, but i have one last question to ask. 我已经完成了程序,并且运行得很好,但是我还有最后一个问题要问。 I need to count how many times the number 5 appears in my vector - disqualify. 我需要计算5出现在我的向量中的次数-失格。 My code is below, and any help on how to determine how many times 5 appears as a disqualifying prime is greatly appreciated. 我的代码在下面,对于如何确定5作为不合格素数出现多少次的任何帮助,我们将不胜感激。 I can only get the contents of the entire vector to appear so im at a loss. 我只能使整个向量的内容显得茫然无措。 Thank you! 谢谢!

#include <iostream>
#include <iomanip>
#include <vector>
#include <iterator>
using namespace std;

int main()
{
    const int MAX(100);         // Number of primes to be identified
    long primes[MAX] = { 2, 3, 5 }; // Initialize with first three primes
    long trial(5);              // Hold a candidate prime
    int count(3);               // Count primes found - reflects initial values
    bool found(false);          // Indicates when a prime is found

    vector <long> nonPrimes; //Vector to hold non prime numbers
    vector <long> disqualify; //Vector to hold prime numbers that disqualify the non prime numbers as prime numbers. 
    vector<long>::iterator fives;//Vector iterator which will be used to find how many times the number 5 disqualifies a nonPrime number.

    do
    {
        trial += 2;               // Produce next candidate value
        found = false;            // Reset indicator - assume it is not prime

        for (int i = 0; i < count; i++)   // Try division by existing primes
        {
            found = (trial % primes[i]) == 0; // True if no remainder
            if (found) // No remainder means number is not a prime
                {
                nonPrimes.push_back(trial); // push_back trial values to vector nonPrimes
                disqualify.push_back(primes[i]); //push back disqualifying prime numbers to disqualify vector
                break;     // Terminate the division loop
                }
            }
        // The above loop will exit either due to a break or after trying all
        // existing primes as a divisor. found was initialized to false. If
        // found is false after exiting the loop, a divisor was not found.
        if (!found)               // We have a new prime: found = true! -- add numbers to vectors
            primes[count++] = trial;  // Save candidate in next array position
    } while (count < MAX);

    // Main loop has completed - we have found MAX prime numbers.
    // Display the prime numbers, presenting five numbers on one line.
    cout << "Prime numbers found during the program execution:" << endl;
    for (int i = 0; i < MAX; i++)
    {
        if (i % 5 == 0)          // For a new line on first line of output
            cout << endl;         // and on every fifth line that follows
        cout << setw(10) << primes[i];  // Provide space between numbers
    }
    cout << endl;              // All primes displayed - for a new line


    /*
        Display Non-primes and their disqualifiers
    */
    cout << "Non-Primes identified: " << count << endl; // Identify how many nonprimes appear and display the value.
    for (int i = 0; i < MAX; i++) //For loop to clarify position of output
    {
        if (i % 5 == 0)          // For a new line on first line of output
            cout << endl;         // and on every fifth line that follows
        cout << setw(10) << nonPrimes[i] << ":" << disqualify[i] << setw(10);   // outputs nonPrime numbers and their disqualifying primes
    }
    cout << endl;

    //Use iterator (fives) to produce how many times the digit 5 appears as a disqualifying prime number
    for (fives = disqualify.begin(); fives < disqualify.end(); fives++) // bounds checking for how many times 5 appears.
    {
            cout << "Numer of times 5 was a disqualifier: " << *fives << endl; // output number of times 5 appears as a disqualifying prime
    }
    system("Pause");

    return 0;
}

If I understand the question correctly, the last few lines are almost right. 如果我正确理解了这个问题,那么最后几行几乎是正确的。

//Use iterator (fives) to produce how many times the digit 5 appears as a disqualifying prime number
for (fives = disqualify.begin(); fives < disqualify.end(); fives++) // bounds checking for how many times 5 appears.
{
        cout << "Numer of times 5 was a disqualifier: " << *fives << endl; // output number of times 5 appears as a disqualifying prime
}

This loop says to run one-by-one through the elements of disqualify and print "Number of times 5 was a disqualifier: " followed by the element at that point in disqualify . 该循环表明,要逐一遍历disqualify元素并显示“ Number of 5 is a disqualifier:Number of 5 was a disqualifier:”,然后是那一元素在disqualify中

You can simply change that loop to count the fives by incrementing a counter every time it hits a 5, then put the print afterward like so (I changed the name of fives to ittr , a generic iterator name, to clear up confusion): 你可以简单地改变环路由每次击中5次递增计数器计数击掌,然后把打印之后,像这样(我改的名字fivesittr ,一个通用的迭代名,清理混乱):

int five_count = 0;
vector<long>::iterator ittr;
for (ittr = disqualify.begin(); ittr < disqualify.end(); ittr++)
{
    if (*ittr == 5)
        ++five_count;
}
cout << "Number of times 5 was a disqualifier: " << five_count << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM