我正在用C++編寫一個並行素數因子分解程序。我設法搞定了所有的線程,並且發現了非常好的主題,但是它的結局我似乎無法得到。當用戶輸入多個數字來查找素數因子時,它將打印整個質數因子分解的整個數組。我希望它只打印與唯一編號相關的主要因素。重印二維數組素數因子
我想將其更改爲後再行「的10因式分解是」不打印素數的整向量。所有打印都發生在主功能的底部。要非常具體的,如果我在兩個10的輸入,輸出應該是:
---所需的輸出---
「的10因式分解是」
「2 5 「
」的10的素數因數分解是「
」2 5「
--- /期望的輸出---
不用擔心「存在:0個素數」部分。我知道如何修復已經
任何和所有的幫助表示讚賞!
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <mutex>
#include <list>
#include <algorithm>
using namespace std;
using namespace std::chrono;
int userInput; // This number is used to help store the user input
vector<long long> vec(0); // A vector storing all of the information
int numPrimes; // Used to count how many prime numbers there are
bool PRINT = false; // lets me decide if I want to print everything for debugging purposes
int arraySize;
vector<thread> threads;
vector<vector<long long> > ending;
void getUserInput()
{
//while the user has not entered 0, collect the numbers.
cout << "Please enter a number for prime factorization. Enter 0 to quit" << endl;
do
{
cin >> userInput;
if (userInput != 0)
{
vec.push_back(userInput);
arraySize++;
}
} while (userInput != 0);
}
vector<long long> primeFactors(long long n)
{
vector<long long> temp;
while (n % 2 == 0)
{
temp.push_back(n);
numPrimes++;
n = n/2;
}
for (int i = 3; i <= sqrt(n); i = i + 2)
{
while (n%i == 0)
{
temp.push_back(n);
numPrimes++;
n = n/i;
}
}
if (n > 2)
{
temp.push_back(n);
numPrimes++;
}
return temp;
}
void format()
{
cout << endl;
}
bool isPrime(long long number){
if (number < 2) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
for (int i = 3; (i*i) <= number; i += 2){
if (number % i == 0) return false;
}
return true;
}
vector<long long> GetPrimeFactors(long long num)
{
vector<long long> v;
for (int i = 2; i <= num; i++)
{
while (num % i == 0)
{
num /= i;
v.push_back(i);
}
}
return v;
}
int main()
{
// how to find out how many cores are available.
getUserInput();
high_resolution_clock::time_point t1 = high_resolution_clock::now();
// vector container stores threads
format();
for (int i = 0; i < arraySize; ++i)
{
vector<long long> temp;
threads.push_back(thread([&]
{
ending.push_back(GetPrimeFactors(vec.at(i)));
}));
}
// allow all of the threads to join
for (auto& th : threads)
{
th.join();
}
for (int i = 0; i < arraySize; ++i)
{
cout << "The prime factorization of " << vec.at(i) << " is \n" << endl;
for (int m = 0; m < ending.size(); m++)
{
vector<long long> v = ending[m];
for (int k = 0; k < v.size(); k++)
{
cout << v.at(k) << " ";
}
}
cout << endl;
}
format();
cout << "There are: " << numPrimes << " prime numbers" << endl;
//time
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(t2 - t1).count();
format();
cout << "Time in seconds: " << (duration/1000000.0) << endl;
format();
}
你發現了什麼?你能解釋一下嗎? – 0x499602D2 2014-10-03 02:38:27
對不起。我改變的是for循環,其中包含cout「來自原始代碼的打印輸出語句的主要因式分解」。我改變它不做雙打印。所以邏輯是因爲矢量結束只是一個2D矢量,我會傳遞一個名爲'temp'的臨時矢量,幷包含從GetPrimeFactors()收到的所有素數。這總是按順序打印。 – booky99 2014-10-03 19:15:32