ac了,但更迷茫了,不知道原代码错在哪
查看原帖
ac了,但更迷茫了,不知道原代码错在哪
1523816
thankyou158楼主2024/11/20 18:35

如题,原本是60分两个wa都是输出大了。最后比着题解把读入拖进切割的for循环里,莫名对了。

#include <stdio.h>
int a[30][30][30] = {0};
int x1[22], y1[22], z1[22], x2[22], y2[22], z2[22];
int main() {
	int w, d, h;
	scanf("%d %d %d", &w, &d, &h);//读入长宽高
	int q;
	scanf("%d", &q);
	for (int i = 0; i < q; i++) {//读入切割数据
		scanf("%d %d %d %d %d %d", &x1[i], &y1[i], &z1[i], &x2[i], &y2[i], &z2[i]);
	}
for (int i = 0; i < q; i++) {//遍历切割
	for (int x = x1[i]; x <= x2[i]; x++) {
	for (int y = y1[i]; y <= y2[i]; y++) {
	for (int z = z1[i]; z <= z2[i]; z++) {
		a[x][y][z]=1;
				}
			}
		}
	}

	int v = 0;//遍历未切割体积
	for (int x = 1; x <= w; x++) {
		for (int y = 1; y <= d; y++) {
		for (int z = 1; z <= h; z++) {
			if (a[x][y][z] == 0)
					v++;
			}
		}
	}
	printf("%d", v);
	return 0;
}

上为原代码,ac代码就把for循环读入删了,scanf拖进下个for循环就对了。

#include <stdio.h>

int a[30][30][30] = {0};
int x1[22], y1[22], z1[22], x2[22], y2[22], z2[22];
int main() {
	int w, d, h;
	scanf("%d %d %d", &w, &d, &h);
	int q;
	scanf("%d", &q);
	for (int i = 0; i < q; i++) {
		scanf("%d %d %d %d %d %d", &x1[i], &y1[i], &z1[i], &x2[i], &y2[i], &z2[i]);
		for (int x = x1[i]; x <= x2[i]; x++) {
			for (int y = y1[i]; y <= y2[i]; y++) {
				for (int z = z1[i]; z <= z2[i]; z++) {
					a[x][y][z] = 1;
				}
			}
		}
	}

	int v = 0;
	for (int x = 1; x <= w; x++) {
		for (int y = 1; y <= d; y++) {
			for (int z = 1; z <= h; z++) {
				if (a[x][y][z] == 0)
					v++;
			}
		}
	}
	printf("%d", v);
	return 0;
}

望大佬解惑

2024/11/20 18:35
加载中...