2014-10-10 63 views
0

我已经完成了我的程序,它运行得非常好,但我还有最后一个问题要问。 我需要计算数字5出现在我的向量中的次数 - 取消资格。 我的代码如下,并且如何确定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; 
} 
+5

'std :: count(my_vec.begin(),my_vec.end(),my_value);' – 2014-10-10 00:15:33

回答

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 
} 

这个循环说成运行一个接一个通过资格和打印的元素“的5倍数是丧失资格:”,接着在资格该点的元件。

您可以简单地改变该循环由每次击中5次递增计数器计数击掌,然后把打印之后,像这样(我改变了fives名称ittr,一个通用的迭代名,清理混淆):

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;