滑动窗口的经典题型,重复题目
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX(a,b) ((a) > (b) ? (a) : (b))
int main()
{
int n;
scanf("%d", &n);
int *list = malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
scanf("%d", &list[i]);
}
int m;
scanf("%d", &m);
int sum = 0;
int max;
int left = 0, right = 0;
while (right < n) {
sum += list[right];
right++;
if (right - left < m) {
continue;
}
if (left == 0) {
max = sum;
} else {
max = MAX(max, sum);
}
sum -= list[left];
left++;
}
printf("%d\n", max);
return 0;
}