CSP题解|201703-1 分蛋糕|100分

#include <bits/stdc++.h>
using namespace std;

int main() {
	int cakeNum,threshold,num=0;
	cin>>cakeNum>>threshold;
	int cake[cakeNum];
	for(int i =0;i<cakeNum;i++){
		cin>>cake[i];
	}
	//开始分配
	for(int i =0;i<cakeNum;i++){
		int get=0;
		while(get<threshold &&i<cakeNum){
			get+=cake[i];
			i++;
		}
		i--;
		if(get>=0)num++;
	} 
	cout<<num<<endl;
}

i--是因为出循环的时候多加了一次

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务