原题
https://www.luogu.org/problemnew/show/P1008
题解
状压朴素
也就是用二进制代替布尔数组, 然后朴素地给出代码。
#include
using namespace std;
int main()
{
for (int i=192;i<=329;i++){//枚举A最多329
int p=i,j=i<<1,k=i3,t=0;
while § t|=1<<p%10-1,p/=10;//分解,第p%10位为1
while (j) t|=1<<j%10-1,j/=10;//以此类推
while (k) t|=1<<k%10-1,k/=10;
if ((t&511)==511) printf("%d %d %d\n",i,i<<1,i3);//也就是都匹配到了
}
return 0;
}
对while语句的解释:
运算顺序:算术运算-> 移位运算->位运算 (逻辑运算)
p%10是p的个位数字,当每次进入while循环时,认为此时光标总是指示在右边第一位,故左移p%10-1位后即是第p%10位。t|=1是或运算,作用是使该位为1(原来是0,因为t的初值为0).
它一共有9位,所以如果1至9都存在,用二进制表示为2^9-1=511