先前:这是能AC左边和不能AC右边的我自己的答案,仅仅是顺序不一样。
(后面我想到把结果存储,然后排序输出就能解决了)
思路:(暴力解法)思路大概就是第一次只开一盏灯,要么只开第一个,要么只开第二个。。。然后判断四个灯亮的情况,然后第二次只开两盏灯.....直到最后一次全部开关都打开,然后判断四个灯亮,next_permutation函数可以实现字典排序,比如说一个开关的情况这个函数可以一直从000000001~100000000挨着变大。
参考代码:不能AC
#include<bits/stdc++.h> using namespace std; int light[10]; char sw[10]; void switchs(int &a) { if (a == 0) a = 1; else a = 0; } void control(int a) { if (a == 1) { switchs(light[2]); switchs(light[4]); } if (a == 2) { switchs(light[1]); switchs(light[3]); switchs(light[5]); } if (a == 3) { switchs(light[2]); switchs(light[6]); } if (a == 4) { switchs(light[1]); switchs(light[5]); switchs(light[7]); } if (a == 5) { switchs(light[2]); switchs(light[4]); switchs(light[6]); switchs(light[8]); } if (a == 6) { switchs(light[3]); switchs(light[5]); switchs(light[9]); } if (a == 7) { switchs(light[8]); switchs(light[4]); } if (a == 8) { switchs(light[5]); switchs(light[7]); switchs(light[9]); } if (a == 9) { switchs(light[6]); switchs(light[8]); } } int main() { int summm = 0; for (int k = 1; k < 10; k++) { memset(sw, '0', sizeof(sw)); for (int j =9; j>9-k; j--) { sw[j] = '1'; } do { memset(light, 0, sizeof(light)); for (int i = 1; i < 10; i++) { if (sw[i] == '1') control(i); } int count = 0; for (int i = 1; i < 10; i++) { if (light[i] == 1) count++; } if (count == 4) { summm++; for (int i = 1; i < 10; i++) { cout << sw[i]; if (i == 9) cout << endl; } } } while (next_permutation(sw + 1, sw + 10)); } cout << summm; }
能AC:(加了个排序);
#include<bits/stdc++.h> using namespace std; int light[10]; int ans[1000]; int an = 0; char sw[10]; void switchs(int &a) { if (a == 0) a = 1; else a = 0; } void control(int a) { if (a == 1) { switchs(light[2]); switchs(light[4]); } if (a == 2) { switchs(light[1]); switchs(light[3]); switchs(light[5]); } if (a == 3) { switchs(light[2]); switchs(light[6]); } if (a == 4) { switchs(light[1]); switchs(light[5]); switchs(light[7]); } if (a == 5) { switchs(light[2]); switchs(light[4]); switchs(light[6]); switchs(light[8]); } if (a == 6) { switchs(light[3]); switchs(light[5]); switchs(light[9]); } if (a == 7) { switchs(light[8]); switchs(light[4]); } if (a == 8) { switchs(light[5]); switchs(light[7]); switchs(light[9]); } if (a == 9) { switchs(light[6]); switchs(light[8]); } } int main() { for (int k = 1; k < 10; k++) { memset(sw, '0', sizeof(sw)); for (int j = 9; j > 9 - k; j--) { sw[j] = '1'; } do { memset(light, 0, sizeof(light)); for (int i = 1; i < 10; i++) { if (sw[i] == '1') control(i); } int count = 0; for (int i = 1; i < 10; i++) { if (light[i] == 1) count++; } if (count == 4) { ans[an++] = atoi(sw + 1); } } while (next_permutation(sw + 1, sw + 10)); } sort(ans, ans + an); for (int i = 0; i < an; i++) { printf("%09d\n", ans[i]); } }
0.0分
3 人评分
C语言程序设计教程(第三版)课后习题6.6 (C++代码)浏览:694 |
C语言程序设计教程(第三版)课后习题7.2 (C语言代码)浏览:521 |
简单的a+b (C语言代码)浏览:707 |
C语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:603 |
拆分位数 (C语言代码)浏览:1322 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:498 |
C语言程序设计教程(第三版)课后习题5.7 (Java代码)浏览:884 |
WU-蓝桥杯算法提高VIP-企业奖金发放 (C++代码)浏览:1158 |
简单的a+b (C语言代码)浏览:625 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:326 |