编程之美之1.2 中国象棋将帅问题
来源:互联网 发布:淘宝信用度怎么看 编辑:程序博客网 时间:2024/06/05 04:09
解法1: 主要运用了 位运算,很巧妙;
#include <stdio.h>#include<windows.h>#define HALF_BITS_LENGTH 4#define FULLMASK 255#define LMASK (FULLMASK <<HALF_BITS_LENGTH)#define RMASK (FULLMASK >>HALF_BITS_LENGTH)#define RSET(b,n) (b = ((LMASK & b )| (n)))#define LSET(b,n) (b = ((RMASK & b )| ((n) <<HALF_BITS_LENGTH)))#define RGET(b) (RMASK & b)#define LGET(b) ((LMASK & b) >> HALF_BITS_LENGTH)#define GRIDW 3int main(){ unsigned char b = 0; 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) % GRIDW != RGET(b) % GRIDW) printf("A = %d, B = %d\n",LGET(b),RGET(b)); } } system("pause"); return 0;}
解法2:解法技巧性很高
int i = 81;while(i--){ if(i/9%3 == i%9%3) continue; printf("A = %d,B = %d\n",i/9+1,i%9+1);}
解法3:
struct { 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);
阅读全文
0 0
- 编程之美 1.2 中国象棋将帅问题
- 编程之美1.2 中国象棋将帅问题
- 编程之美 1.2 中国象棋将帅问题
- 1.2-编程之美-中国象棋将帅问题
- 编程之美 1.2 中国象棋将帅问题
- 编程之美1.2中国象棋将帅问题
- <<编程之美>>之中国象棋将帅问题
- 编程之美之中国象棋将帅问题
- 编程之美之中国象棋将帅问题
- 编程之美 - 中国象棋将帅问题
- 编程之美 - 读书笔记 - 中国象棋将帅问题
- 编程之美:中国象棋将帅问题
- 微软编程之美--中国象棋将帅问题
- 编程之美-中国象棋将帅问题
- 编程之美_002中国象棋将帅问题
- 编程之美-中国象棋将帅问题
- 编程之美---中国象棋将帅问题
- 编程之美二 : 中国象棋将帅问题
- Mysql如何支持Emoji表情,修改数据库字符集为utf8mb4后依然不行
- easyui之重新加载datagrid
- jquery easyui datagrid 获取Checked选择行(勾选行)数据
- Mybatis中模糊查询
- Mybatis实现批量删除操作
- 编程之美之1.2 中国象棋将帅问题
- MyBatis传入多个参数的问题
- boa cgi + web页实现文件的上传
- easyui+spring MVC+Mybatis实现分页查询
- spring中datasource远程访问数据库密码配置
- 深度卷积神经网络在目标检测中的进展
- 海康摄像头RTSP
- 分页工具类
- Spring MVC学习历程<一>Spring MVC简介