先前:这是能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语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:262 |
图形输出 (C语言代码)浏览:1375 |
震宇大神的杀毒软件 (C语言代码)浏览:1079 |
剪刀石头布 (C语言代码)浏览:1432 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:463 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:512 |
求圆的面积 (C语言代码)浏览:657 |
检查金币 (C语言代码)浏览:1412 |
字符逆序 (C语言代码)求大神指出错处,运行结果尝试了也与要求一样,但就是说结果错误,不知错在哪里浏览:436 |
A+B for Input-Output Practice (C语言代码)浏览:466 |