我已经完成了我的程序,它运行得非常好,但我还有最后一个问题要问。 我需要计算数字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;
}
答案 0 :(得分: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元素逐个运行并打印“5次为反对者的次数:”,然后是元素在此次取消资格。
您可以通过在每次点击5时递增计数器来简单地更改该循环以计算五位数,然后将打印件放入其后(我将fives
的名称更改为ittr
,a通用迭代器名称,以消除混淆):
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;