shivakumar
BAN USERIts for efficiency purpose push_front doesn't exist.
Vector is a contiguous memory location adding element to front requires shifting all the elements to right by 1 position.
There might be many ways to solving this problem but main idea is to identify poisonous bottle with minimum slaves and time.
My solution will take 10 prisoner and 1hour time.
Name 10 prisoners as P0 to P10
Name 1000 bottles in binary Ex bottle 1 as 0001, bottle as 0101....bottle 10 as 1010 etc
Ask prisoner P0 to drink from Bottle 1 (00000000001)
Ask prisoner P1 to drink from Bottle 1 and 2 (00000000001,00000000010)
.
.
.
Ask prisoner P10 to drink from Bottle 1...Bottle1000
Finally after 1 hour see which prisoner has died suppose if prisoner 1,4 has died the poisoned bottle is 5.
This solution will even hold good for 1024 prisoners.
Make destructor of class private.
Write a friend function which takes the ownership if deleting object.
#include <iostream>
using namespace std;
class aa
{
public:
aa()
{
}
friend void del(aa* obj)
{
delete obj;
}
private:
~aa(){}
};
int main() {
// your code goes here
aa *a = new aa();
//delete a;
del(a);
return 0;
}
Use condition variables to sleep/wake threads based on condition
- shivakumar May 11, 2014