Bingsuns
BAN USERI will use an array as internal buffer, the capacity will be decided in ctor.
class cqueue
{
public:
cqueue(int);
~cqueue();
public:
bool enqueue(ITEM*);
ITEM* dequeue();
private:
ITEM* buffer;
int capacity;
int head;
int size;
};
cqueue::cqueue(int n)
{
size = head = 0;
buffer = NULL;
if(capactity<=0)
return;
capacity = n;
buffer = new ITEM[n];
if(buffer = NULL)
capacity = 0;
}
bool
cqueue::enqueue(ITEM* it)
{
if(size == capacity)
return false;
buffer[(head+size)%capacity] = it;
++size;
return true;
}
ITEM*
cqueue::dequeue()
{
if(size == 0)
return NULL;
--size;
ITEM* it = buffer[head];
head = (head+1)%capacity;
return it;
}
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <assert.h>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
int i = 0;
cin>>i;
int index = 0, temp = i;
if(temp&0xffff0000)
{
index += 16;
temp = (temp&0xffff0000)>>16;
}
if(temp&0x0000ff00)
{
index += 8;
temp = (temp&0x0000ff00)>>8;
}
if(temp&0x000000f0)
{
index += 4;
temp = (temp&0x000000f0)>>4;
}
if(temp&0x0000000c0)
{
index += 2;
temp = (temp&0x0000000c0)>>2;
}
if(temp&0x00000002)
++index;
cout<<index<<endl;
return 0;
}
//geniusxsy's post actually said all, I just want to check if I can finish it in 15mins
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <assert.h>
#include <algorithm>
using namespace std;
//Q:
//Write a function to generate all possible n pairs of balanced parentheses. For example, if n=1
//{}
//for n=2
//{}{}
//{{}}
void pairs(int);
void pairs(int, int, int, string); //aux function for recursive
int main(int argc, char* argv[])
{
int n = 0;
while(n<=0)
cin>>n;
pairs(n);
return 0;
}
void pairs(int n)
{
pairs(n, n, 0, "");
}
void pairs(int left, int right, int diff, string s)
{
if(left == 0 && right == 0)
{
cout<<s<<endl;
return;
}
if(diff == 0)
{
pairs(left-1, right, diff+1, s+'{');
return;
}
else
{
if(left>0)
pairs(left-1, right, diff+1, s+'{');
pairs(left, right-1, diff-1, s+'}');
}
}
well, since it's for children under 6 yrs old, I would put 5 buttons there:
On, Off, V Up, V down, Channel Change.
And in "channel change", only channels for children are allowed (the channel can hard coded in the controller).
Cuz I am not grow up here, so I am not sure how many channels are suitable for children under 6, if only 3, then 3 button with the major cartoon charactor might be good as well (of course this can limit the extendability later when the kids are 7 yrs old:) ).
What about std::map?
- Bingsuns January 30, 2010//data structure we need
map<char*, string> opts1; //the opts which take arguments
map<char*, int> opts2; //the opts which doesn't need arguments, e.g, -R
//algorithm
step 1: first insert all the legal opts into two maps
step 2: parse the input, if illegal or duplicate opts found, return false
step 3: set the corresponding value