naymenshee find common multiple of 10-million numbers in the queue does not exceed 10,000 I killed 2 days to sort out but I just do not understand! please help me
#include <condition_variable>
#include <mutex>
#include <thread>
#include <iostream>
#include <queue>
#include <chrono>
#include <cmath>
#include <map>
#include <cstdlib>
#include <fstream>
#include <ctime>
using namespace std;
int main()
{
std::map <int, int> NOK;
map<int, int> snok;
std::queue<int> oche;
std::mutex m;
std::condition_variable cond_var;
bool done = false;
bool notified = false;
int ch=0;
int d=0;
int t=0;
int s[2000];
// int rnd=0;
std::thread filev([&]() {
//std::this_thread::sleep_for(std::chrono::seconds(1));
//std::unique_lock<std::mutex> lock(m);
ifstream in; // Поток in будем использовать для чтения
int ch;
in.open("/home/akrasikov/prog/output.txt");
while(!in.eof()){
if (oche.size()>9999){
std::this_thread::sleep_for(std::chrono::milliseconds(3));
std::unique_lock<std::mutex> lock(m);
} else {
in>>ch;
oche.push(ch);
}
//cout << ch << " ";
}
//rnd=rand()%100;
// oche.pop();
notified = true;
cond_var.notify_one();
done = true;
cond_var.notify_one();
});
std::thread nok([&]() {
std::unique_lock<std::mutex> lock(m);
//while (!done) {
// while (!notified) { // loop to avoid spurious wakeups
//cond_var.wait(lock);
//}
while (!oche.empty()) {
ch=oche.front();
oche.pop();
int j=2;
while (j < sqrt((double)ch)+1){
int s=0;
while(!(ch%j)){
s++;
ch/=j;
// std::cout << j << std::endl;
}
if (s > 0 && NOK[j] < s){
NOK[j] = s;
}
j++;
}
if (NOK[ch] == 0) NOK[ch]++;
// std::cout << v << std::endl;
}
long int su=1;
int temp=-1;
int step=0;
//int st=0;
int sa=1;
//int t=0;
std::cout << " NOK= ";
for (std::map<int, int>::iterator it=NOK.begin(); it!=NOK.end(); it++){
for (int i=0; i<it->second; i++){
su*=it->first;
sa=it->first;
//cout << "sa= " << sa << "\n";
if (temp<sa && sa >1){
// cout << "sa= " << sa << "\n";
// cout << "temp=" << temp<< "\n";
//st=step;
//cout<< temp << "^"<< step << " * " ;
temp=sa;
step=1;
//cout<< temp << "^"<< step << " * " ;
} else {
if(sa>1)
step++;
}
//s[i]=it->first;
//if (s[i]==2) d++;
//if (s[i]==3) t++;
//cout<< temp << "^"<< step << " * " ;
}
//if(t>0)
cout<< temp << "^"<< step << " * " ;
//t++;
}
//cout<< temp << "^"<< step << " * " ;
//std::cout << "NOK= 2^"<< d << " 3^" << t << std::endl;
std::cout << "su = " << su << '\n';
//oche.pop();
//}
notified = false;
});
filev.join();
nok.join();
}
This program does not work! how come? what's wrong? it just starts and hangs, but if you do not clean
if (oche.size()>9999){
std::this_thread::sleep_for(std::chrono::milliseconds(3));
std::unique_lock<std::mutex> lock(m);
} else {
and
while (!done) {
while (!notified) { // loop to avoid spurious wakeups
cond_var.wait(lock);
}
everything works help plz
Aucun commentaire:
Enregistrer un commentaire