算出每个窗户左上角和右下角的坐标,直接模拟就行了。
这题直接把sum放在G数组后面,导致sum变成了char型,错了一次,G数组第一次只开了105~~~。
代码如下:
#include#include #include using namespace std;char G[600][600];int sum[10];int main(){ int N, M, x1, y1, x2, y2, cnt; while (scanf("%d %d", &N, &M) == 2) { memset(sum, 0, sizeof (sum)); for (int i = 0; i < 5 * N + 1; ++i) { scanf("%s", G[i]); } for (int i = 0; i < N; ++i) { for (int j = 0; j < M; ++j) { cnt = 0; x1 = 5 * i + 1; y1 = 5 * j + 1; x2 = 5 * i + 4; y2 = 5 * j + 4; for (int p = x1; p <= x2; ++p) { for (int q = y1; q <= y2; ++q) { if (G[p][q] == '*') { ++cnt; } } } ++sum[cnt/4]; } } for (int i = 0; i < 5; ++i) { printf(i == 0 ? "%d" : " %d", sum[i]); } puts(""); } return 0;}