14th蓝桥杯省赛JavaA组 填空题2:与或异或
作者:
singleApo
,
2024-04-11 21:23:46
,
所有人可见
,
阅读 6
public class Main {
static int ans = 0;
static int[] way = new int[11];
static int[][] a = new int[6][6];
public static int cal(int x, int y, int op) {
if (op == 1)
return x & y;
if (op == 2)
return x | y;
if (op == 3)
return x ^ y;
return -1;
}
public static void dfs(int x) {
if (x > 9) {
int k = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4 - i; j++)
a[i + 1][j] = cal(a[i][j], a[i][j + 1], way[k++]);
if (a[4][0] == 1)
ans++;
return;
}
way[x] = 1;
dfs(x + 1);
way[x] = 0;
way[x] = 2;
dfs(x + 1);
way[x] = 0;
way[x] = 3;
dfs(x + 1);
way[x] = 0;
}
public static void main(String[] args) {
a[0][0] = 1;
a[0][1] = 0;
a[0][2] = 1;
a[0][3] = 0;
a[0][4] = 1;
dfs(0);
System.out.print(ans);
}
}