作者:vfh1150403 | 来源:互联网 | 2023-07-03 06:32
1 中国围棋将帅问题
![题干](https://img8.php1.cn/3cdc5/1538d/4aa/a101590cdfa2a39a.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMxMzQ5Njgz,size_16,color_FFFFFF,t_70)
1.1 主要思路
使用一个字节,自然想到利用字符变量来存储数据。要保存A、B两个棋盘的信息,可将字符变量拆分为前后两个4bit数据,分开进行读写。将每个位置分别使用1-9表示,如下所示:
![棋盘](https://img8.php1.cn/3cdc5/1538d/4aa/49ea407154db1a3f.png)
1.2 实现代码
#include
#define LGET(b) (b >> 4)
#define LSET(b,n) ( b &#61; ((b & 0x0f) | (n <<4) ))
#define RGET(b) (b & 0x0f)
#define RSET(b,n) ( b &#61; (b & 0xf0) | n )int main() {unsigned char b;for(LSET(b, 1); LGET(b) < 10; LSET(b, LGET(b) &#43; 1)) {for (RSET(b, 1); RGET(b) < 10; RSET(b, RGET(b) &#43; 1))if (LGET(b) % 3 !&#61; RGET(b) % 3) { printf("A&#61;%d, B&#61;%d\n", LGET(b), RGET(b));}}return 0;
}