Is this not solving my first report? Although the algorithm is not a problem.
The original question: fill in the blank program to achieve a STL bitset in the MyBitset, specify the output results are similar.
#include <iostream>
#include <cstring>
using namespace std;
template <int bitNum>
struct MyBitset
{
char a[bitNum/8+1];
MyBitset() { memset(a,0,sizeof(a));};
void Set(int i,int v) {
char & c = a[i/8];
int bp = i % 8;
if( v )
c |= (1 << bp);
else
c &= ~(1 << bp);
}
// 在此处补充你的代码
void Print() {
for(int i = 0;i < bitNum; ++i)
cout << (*this) [i];
cout << endl;
}
};
int main()
{
int n;
int i,j,k,v;
while( cin >> n) {
MyBitset<20> bs;
for(int i = 0;i < n; ++i) {
int t;
cin >> t;
bs.Set(t,1);
}
bs.Print();
cin >> i >> j >> k >> v;
bs[k] = v;
bs[i] = bs[j] = bs[k];
bs.Print();
cin >> i >> j >> k >> v;
bs[k] = v;
(bs[i] = bs[j]) = bs[k];
bs.Print();
}
return 0;
}
Input
a plurality of sets of data
each set of data:
The first line is an integer of n, 1 <= n <20 ;
second line n integers k1, k2 ... kn, are in the range [0, 19] within.
The third line is four integers i1, j1, k1, v1. 0 <= i1, j1, k1 <= 19, v1 is 0 or 1.
The third line is the integer four i2, j2, k2, v2. 0 <= i2, j2, k2 <= 19, v2 is 0 or 1
output
for each set of data, the common output lines 3, each row 20, each is 0 or 1. 0 leftmost referred to as
the first line: the first k1, k2 ... kn bit is 1, the remaining bits to 0.
The second line: the first i1, j1, k1 bits in the first row becomes v1, the other bits unchanged
Third row: the first bit in the second row i2 k2 and bit becomes v2, the other bits unchanged
Sample input
. 4
0 2. 1. 8
. 7. 1. 19 0
. 7 0 2. 8
. 1
. 1
. 1. 1. 1 0
. 1. 1. 1. 1
sample output
11100000100000000000
11100001100000000001
11100000000000000001
01000000000000000000
00000000000000000000
01000000000000000000
tips
recommended inner classes, using the class referenced inner member. To initialize a reference member in the constructor.
I started tangled point where is it, I do not know how to remove the references from a bit of char. But after I read the source code bitset know where I sucker. We simply removed one char in the char array, then the position will be recorded, and the time change can be realized by a one-bit arithmetic operation among other functions. Set functions that actually is a good tip.
Then the second point I tangled using pointers or references to the subject. Found inside the reference class can not access the char array outside the class. So I set up a pointer p to point to the internal class, it cast a pointer outer class - of course sucker operation because the inner class and the outer class basically no logical connection, and this inheritance and derived are essentially different.
Later, a reference to other people's code was aware of the problem, in the constructor added a reference among the parameters, that is, MyBitset & p. Heavy-duty [] directly back to the time reference (position, * this).
The last point ...... debug a sucker for an hour, that I put all types of functions are habitually set reference &, resulting in a time of assignment, = the right reference object position was changed to the left of the reference object = position, which makes it impossible assignment. Should return the reference object in heavy = when a replica, not a reference to it.
It seems to be some very basic questions before knowledge among these are, but not forgotten a diligent review leads.
Finally, although written for several hours again ...... but AC is also very happy. AC code is as follows:
class reference {
public:
char *x;
MyBitset *p;
int pos;
reference() {}
reference (int i,MyBitset &p) {
x = &(p.a[i / 8]);
pos = i;
}
reference operator =(int v) {
if (v)
*x |= (1 << (pos % 8));
else
*x &= ~(1 << (pos % 8));
return *this;
}
reference operator =(reference v) {
int tmp = (((*v.x)&(1 << (v.pos % 8))) >> (v.pos % 8));
if (tmp)
*x |= (1 << (pos % 8));
else
*x &= ~(1 << (pos % 8));
return *this;
}
};
reference operator [](int i) {
return reference(i,*this);
}
friend ostream & operator <<(ostream &cout, const reference &m) {
int tmp = (((*m.x)&(1 << (m.pos % 8))) >> (m.pos % 8));
cout << tmp;
return cout;
}