#include <stdio.h> //#include <windows.h> 包含byte #include <IOSTREAM> using namespace std; #define HALF_BIT_LENGTH 4 #define FULLMASK 255 #define LMASK (FULLMASK << HALF_BIT_LENGTH) #define RMASK (FULLMASK >> HALF_BIT_LENGTH) #define RSET(b,n) (b=((b&LMASK)^n)) #define LSET(b,n) (b=((b&RMASK)^(n << HALF_BIT_LENGTH))) #define RGET(b) (b&RMASK) #define LGET(b) ((b&LMASK)>>HALF_BIT_LENGTH) #define GRIDW 3 int main(int argc,char* argv[]) { //byte <==> unsigned char unsigned char b; for(LSET(b,1);LGET(b) <= GRIDW*GRIDW;LSET(b,(LGET(b)+1))) for(RSET(b,1);RGET(b)<=GRIDW*GRIDW;RSET(b,(RGET(b)+1))) if(LGET(b)%3!=RGET(b)%3) //printf("A=%d,B=%d",LGET(b),RGET(b)); cout<<"A="<<RGET(b)<<" "<<"B="<<LGET(b)<<endl; return 0; }
#include <iostream> using namespace std; int main(int argc,char* argv[]) { unsigned char i=81; while(i--) { if(i/9%3!=i%9%3) cout<<"A="<<(i/9+1)<<" "<<"B="<<(i%9+1)<<endl; } return 0; }
#include <stdio.h> int main(int argc,char* argv[]) { struct i{ unsigned char a:4; unsigned char b:4; }i; for(i.a=1;i.a<=9;i.a++) for(i.b=1;i.b<=9;i.b++) if(i.a%3!=i.b%3) printf("a=%d,b=%d\n",i.a,i.b); return 0; }
最后一种方法采用了位域的方法来节省空间,注意输出时候不能使用cout了,会出现错误!
接下来介绍一下位域: