方案1
#include <iostream>
#include <cstdio>
using namespace std;
const int N1 = 1000;
const int N2 = 10000;
int a[N1 + 10];
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int sum = 0;
int cnt = 0;
for (int i = 0; i < n; ) {
sum = 0;
while (sum < k && i < n) {
sum += a[i++];
}
if (sum >= k || i >= n) {
cnt++;
}
}
printf("%d\n", cnt);
return 0;
}
方案2
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int n, k;
scanf("%d%d", &n, &k);
int weight, sum = 0, cnt = 0;
while (n--) {
scanf("%d", &weight);
sum += weight;
if (sum >= k) {
cnt++;
sum = 0;
}
}
printf("%d\n", sum > 0 ? ++cnt : cnt);
return 0;
}