方法1:使用按位与运算
假设0表示不去,1表示去,很显然这是一个二进制数,因此可以使用按位与运算求解,如果二进制数
#include int main() { int a, b, c, d, e, f; for (int i = 0; i < 64; i++) // 2^6 = 64 { // 将整数i右移后在与1进行按位与运算,然后把每一位分别赋值给a, b, c, d, e, f a = (i >> 5) & 1; b = (i >> 4) & 1; c = (i >> 3) & 1; d = (i >> 2) & 1; e = (i >> 1) & 1; f = i & 1; // printf("%d %d %d %d %d %d\n", a, b, c, d, e, f); // 检查条件是否满足 if ((a || b) && (a + d < 2) && (a + e + f == 2) && (b - c == 0) && (c + d == 1) && ((d == 0 && e == 0) || d == 1)) { // 如果条件满足,则打印变量值 // printf("%d %d %d %d %d %d\n", a, b, c, d, e, f); if (a == 1) printf("A,"); if (b == 1) printf("B,"); if (c == 1) printf("C,"); if (d == 1) printf("D,"); if (e == 1) printf("E,"); if (f == 1) printf("F,"); } } return 0; }
方法2:用6个循坏暴力求解
#include int main() { int A, B, C, D, E, F; int a, b, c, d, e, f; for (A = 0; A < 2; A++) { for (B = 0; B < 2; B++) { for (C = 0; C < 2; C++) { for (D = 0; D < 2; D++) { for (E = 0; E < 2; E++) { for (F = 0; F < 2; F++) { if ((A || B) && (A + D < 2) && (A + E + F == 2) && (B - C == 0) && (C + D == 1) && ((D == 0 && E == 0) || D == 1)) { a = A, b = B, c = C, d = D, e = E, f = F; // printf("%d %d %d %d %d %d\n", A, B, C, D, E, F); } } } } } } } if (a == 1) printf("A,"); if (b == 1) printf("B,"); if (c == 1) printf("C,"); if (d == 1) printf("D,"); if (e == 1) printf("E,"); if (f == 1) printf("F,"); return 0; }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题8.9 (C语言代码)浏览:685 |
C语言训练-计算1977!* (C++代码)浏览:900 |
【出圈】 (C语言代码)浏览:582 |
分糖果 (C++代码)浏览:1517 |
程序员的表白 (C语言代码)浏览:689 |
兰顿蚂蚁 (C++代码)浏览:1140 |
C语言程序设计教程(第三版)课后习题8.1 (C语言代码)浏览:562 |
WU-整数平均值 (C++代码)浏览:1292 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:462 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:536 |