201703-1 分蛋糕 Java

时间:2024-08-17 10:34:32

201703-1  分蛋糕 Java

思路:

注意最后如果剩余蛋糕的重量小于k,也算一个人分到

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();//几块蛋糕
int k = sc.nextInt();//要多重的蛋糕
int cakes[] = new int[n];
for(int i=0;i<n;i++) {
cakes[i] = sc.nextInt();
}
int count = 0;
int temp = 0;
for(int j=0;j<n;j++) {
temp += cakes[j];
if(temp >= k) {
count++;
temp = 0;
}
}
if(temp > 0) {
count++;
}
System.out.println(count);
} }