簡體   English   中英

計算向量中出現5的次數

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

我已經完成了程序,並且運行得很好,但是我還有最后一個問題要問。 我需要計算5出現在我的向量中的次數-失格。 我的代碼在下面,對於如何確定5作為不合格素數出現多少次的任何幫助,我們將不勝感激。 我只能使整個向量的內容顯得茫然無措。 謝謝!

#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;
}

如果我正確理解了這個問題,那么最后幾行幾乎是正確的。

//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
}

該循環表明,要逐一遍歷disqualify元素並顯示“ Number of 5 is a disqualifier:Number of 5 was a disqualifier:”,然后是那一元素在disqualify中

你可以簡單地改變環路由每次擊中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