问题分析:这个题需要注意的是当分到最后蛋糕重量不足k时,也要把蛋糕给一个小朋友。
提交后100分的C++程序如下
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int n, k, v, all = 0;
int count = 0;//记录分到蛋糕的小朋友的数量
scanf("%d%d", &n, &k);
while(n--)
{
scanf("%d", &v);
all += v;//all记录小朋友分到蛋糕的重量
if(all >= k)
{
count ++;
all = 0;
}
}
if(all) count ++;
printf("%d\n", count);
return 0;
}