1.2中国象棋将帅问题

#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了,会出现错误!

接下来介绍一下位域:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值