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;
}
'std :: count(my_vec.begin(),my_vec.end(),my_value);' – 2014-10-10 00:15:33