/*
编程之美1.2
中国象棋将帅问题:利用1个字节来输出将帅的合法位置
*/
/*
方法一:位运算
*/
#include <iostream>
using namespace std;
#define LEFT(a) ((a)>>4)
#define RIGHT(a) ((a) & 0x0F)
#define ADD_LEFT(a) a =( (LEFT(a)+1<<4) | RIGHT(a))
#define ADD_RIGHT(a) a = ( (LEFT(a)<<4) | (RIGHT(a)+1))
#define INIT_LEFT(a) a = ( (1<<4) | RIGHT(a) )
#define INIT_RIGHT(a) a = ( (LEFT(a)<<4) | 1 )
#define MATCH(a) (LEFT(a)%3 == RIGHT(a)%3)
int main()
{
//freopen("in.txt", "r", stdin);
unsigned char v = 0x11;
for(INIT_LEFT(v); LEFT(v)<=9; ADD_LEFT(v)) {
for(INIT_RIGHT(v); RIGHT(v)<=9; ADD_RIGHT(v)) if(!MATCH(v)){
cout << "A = " << LEFT(v) << ", B = " << RIGHT(v) << endl;
}
}
return 0;
}
/*
方法二: 位域
*/
#include <iostream>
using namespace std;
struct {
unsigned int a : 4;
unsigned int b : 4;
} i;
int main()
{
//freopen("in.txt", "r", stdin);
for(i.a = 1; i.a <= 9; i.a++) {
for(i.b = 1; i.b <= 9; i.b++) {
cout << "A = " << i.a << ", B = " << i.b << endl;
}
}
return 0;
}
//方法三:十进制
#include <iostream>
using namespace std;
int main()
{
unsigned char i = 0;
for(i = 0; i/10<9; i+=10) {
for(i -= i%10; i%10<9; i++) if(i/10%3 != i%10%3){
cout << "A = " << i/10+1 << ", B = " << i%10+1 << endl;
}
}
return 0;
}
//方法四:九进制 (可以一一对应)
#include <iostream>
using namespace std;
int main()
{
int i = 81;
while(i--) {
if(i/9%3 == i%9%3) continue;
cout << "A = " << i/9 + 1 << ", B = " << i%9 + 1 << endl;
}
return 0;
}