CCF 20170301 Cake

Idea: Data grouping problem, satisfying that the sum of each group of data is greater than or equal to k, except the last group of data.

#include<iostream>

using namespace std;

int main(){
    int n,k;
    cin>>n>>k;
    int ans = 0,val,sub = 0;
    for(int i = 0; i < n; i++){
        cin>>val;
        sub += val
        if(sub >= k){
            ans++;
            sub = 0;
        }
    }
    if(sub > 0){
        ans++;
    }
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/blink-cz/p/12688695.html