4: Self-realization Bitset
Description Program
Fill in the blanks, implement a mybitset similar to STL bitset, output the specified result
#include <iostream>#include<cstring>using namespacestd;template<intBitnum>structMybitset {Chara[bitnum/8+1]; Mybitset () {memset (a),0,sizeof(a));}; voidSet (intIintv) {Char& c = a[i/8]; intbp = i%8; if(v) C|= (1<<BP); ElseC&= ~ (1<<BP); }//Add your code herevoidPrint () { for(inti =0; i < Bitnum; ++i) cout<< (* This) [i]; cout<<Endl; }};intMain () {intN; inti,j,k,v; while(Cin >>N) {mybitset< ->BS; for(inti =0; i < n; ++i) {intT; 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
Multiple sets of data
Each group of data:
The first line is the integer n, 1 <= n < 20;
The second line is n integers k1,k2 ... kn, all within the range [0,19].
The third line is a four integer i1,j1,k1,v1. 0 <= i1,j1,k1 <=, v1 value 0 or 1
The third line is a four integer i2,j2,k2,v2. 0 <= i2,j2,k2 <=, v2 value 0 or 1
Output
For each set of data, a total of 3 rows, 20 bits per row, 1 or 0 per bit. The leftmost is called the No. 0 place.
First line: K1,k2 ... the KN bit is 1 and the remaining bits are 0.
Second line: Change the I1,J1,K1 bit in the first row to V1 and the remaining bits unchanged
Line three: Change the i2 and K2 bits in the second row to V2, and the rest of the bits are not changed into the example input
40 1 2 87 19 0 17 2 8 0111 1 1 01 1 1 1
Sample output
111000001000000000001110000110000000000111100000000000000001010000000000000000000000000000000000000001000000000000000000
Tip It is recommended to use an inner class that uses a reference member in an inner class. The reference member to initialize in the constructor.
Source
Guo Wei
1#include <iostream>2#include <cstring>3 using namespacestd;4Template <intBitnum>5 structMybitset6 {7 CharA[bitnum/8+1];8Mybitset () {memset (a),0,sizeof(a)); };9 voidSet (intIintv) {Ten Char& c = a[i/8]; One intbp = i%8; A if(v) -C |= (1<<BP); - Else theC &= ~ (1<<BP); - } - //Add your code here - classBSP { + Public: - inthext[ -], flag; +BSP () {flag =0; Amemset (Hext,0,sizeof(int) * -); at } - int&operator()(intx,mybitset&k) { - if(!flag) { - for(inti =0; I <= bitnum/8+1; i++) { - intL = k.a[i];intc =8*i; - while(L >0) { in if(l%2) -hext[c]++; toC++; +L/=2; - } the } *Flag =1; $ }Panax Notoginseng returnHext[x]; - } the }; + BSP B; A int&operator[](intx) { the returnB (x,* This); + } - //code ends $ voidPrint () { $ for(inti =0; i < Bitnum; ++i) -cout << (* This) [i]; -cout <<Endl; the } - Wuyi }; the - intMain () Wu { - intN; About intI, J, K, V; $ while(Cin >>N) { -mybitset< ->BS; - for(inti =0; I < n; ++i) { - intT; ACIN >>T; +Bs. Set (T,1); the } - BS. Print (); $Cin >> I >> j >> K >>v; theBS[K] =v; theBs[i] = bs[j] =Bs[k]; the BS. Print (); theCin >> I >> j >> K >>v; -BS[K] =v; in(Bs[i] = bs[j]) =Bs[k]; the BS. Print (); the } About return 0; the}View Code
The fear of being dominated by the sudden end of the game ...
(Why is there a time limit? I thought it would be open before the test.)
(at least until 12, I can save a little bit more.)
(not finished!!!) )
Whether it's right or not, it's probably the next time you open it. (Cool analysis)
18.5.12 C + + optional Title # #