本帖最後由 ray 於 2022-3-27 11:24 編輯
- int maze[5][5]=
- {{1, 1, 1, 1, 1},
- {1, 0, 1, 0, 1},
- {1, 1, 0, 0, 1},
- {1, 0, 0, 1, 1},
- {1, 1, 1, 1, 1} };
- int count=0;
- for (int i=1; i<=3; i=i+1) {
- for (int j=1; j<=3; j=j+1) {
- int dir[4][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}};
- for (int d=0; d<4; d=d+1) {
- if (maze[i+dir[d][0]][j+dir[d][1]]==1) {
- count = count + 1;
- }
- }
- }
- }
複製代碼 -------------------------------------------------------------------------
下列程式片段執行後,count 的值為何?
1. int maze[5][5]= { {1, 1, 1, 1, 1},
2. {1, 0, 1, 0, 1},
3. {1, 1, 0, 0, 1},
4. {1, 0, 0, 1, 1},
5. {1, 1, 1, 1, 1} };
6. int count=0;
7. for (int i=1; i<=2; i=i+1){
8. for (int j=1; j<=2; j=j+1) {
9. int dir[3][2] = {{-1,0}, {0,1}, {1,0}};
10. for (int d=0; d<3; d=d+1) {
11. if (maze[i+dir[d][0]][j+dir[d][1]]==1)
12. {
13. count = count + 1;
14. }
15. }
16. }
17. } |