方法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语言程序设计教程(第三版)课后习题7.3 (C语言代码)浏览:643 |
C语言程序设计教程(第三版)课后习题9.10 (C语言代码)浏览:866 |
核桃的数量 (C语言代码)浏览:893 |
演讲大赛评分 (C语言代码)浏览:1696 |
C语言程序设计教程(第三版)课后习题9.8 (C语言代码)浏览:672 |
2003年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:567 |
IP判断 (C语言代码)浏览:592 |
图形输出 (C语言代码)浏览:1019 |
C语言程序设计教程(第三版)课后习题8.3 (C语言代码)浏览:620 |
JAM计数法 (C语言代码)浏览:721 |