解题思路:把路设为有路长+1的元素的数组,初始化为0代表有树,把后续的坐标当做数组的序号,坐标范围内的赋值1代表没树,最后统计0的数目即可。
注意事项:
参考代码:
#include <stdio.h> #include <stdlib.h> int main() { int count, length; scanf("%d%d", &length, &count); int *road = (int *)calloc(length + 1, sizeof(int)); int (*size)[2] = (int (*)[2])malloc(count * sizeof(int [2])); int sum = 0; for (int i = 0; i < count; i++) { scanf("%d%d", &size[i][0], &size[i][1]); if (size[i][0] > size[i][1]) { int temp = size[i][1]; size[i][1] = size[i][0]; size[i][0] = temp; } } for (int i = 0; i < count; i++) { for (int j = size[i][0]; j <= size[i][1]; j++) { road[j] = 1; } } for (int i = 0; i <= length; i++) { if (road[i] == 0) sum++; } printf("%d", sum); }
0.0分
0 人评分