编程之美-将帅问题

  
#include <stdio.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 3

//使用位段
struct {
	unsigned char a:4;
	unsigned char b:4;
}i;

int main()
{
//位段输出
	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);
//如果j=81到73,j/9:表示商为8
//j%9:对应的余数取1到9
//也即是每一个商对应9个余数,类似与两重遍历
	char j=81;
	while(j--)
	{
		if(j/9 %3 == j%9 %3)
			continue;
//		printf("A=%d,B=%d\n",j/9+1,j%9+1);
	}
//最基本的两重循环,将符合条件的输出
	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)%GRIDW!=RGET(b)%GRIDW)
//				printf("A=%d,B=%d\n",LGET(b),RGET(b));
	return 0;
}
    原文作者:勿悔Choles
    原文地址: https://blog.csdn.net/omashion/article/details/11757649
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞