CCF CSP Brush Question Record 17-201703-1 points cake (java)

Question number: 201703-1
Question name: Divide the cake
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:

Problem Description

  For Xiao Ming’s birthday today, he has n pieces of cake to share with his friends . The weight of these n pieces of cake (numbered 1 to n ) are a 1,  a 2, …,  an . Xiao Ming wants to give each friend a cake that weighs at least k . Xiao Ming’s friends have lined up to get the cake. For each friend, Xiao Ming always gives him the cake with the smallest number in his hand. When the weight of the cake received by this friend is less than k , he will continue to share the remaining cake. Give him the lowest number of the cakes below, until Xiao Ming’s cakes are divided or the total weight of the cakes distributed by this friend is greater than or equal to k .
  May I ask how many friends got the cake when Xiao Ming's cake was divided.

Input format

  The first line of input contains two integers nk , the meaning is as described above.
  The second line contains n positive integers, representing a 1,  a 2, …,  an in turn .

Output format

  Output an integer, which indicates how many friends got the cake.

Sample input

6 9
2 6 5 6 3 5

Sample output

3

Sample description

  The first friend got the first 3 cakes, the second friend got the 4th and 5th cakes, and the third friend got the last cake.

Evaluation use case scale and conventions

  For all evaluation cases, 1 ≤ n  ≤ 1000 , 1 ≤  k  ≤ 10000 , and 1  ≤  ai  ≤ 1000.

 

import java.util.Scanner;
public class 分蛋糕 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int k=sc.nextInt();
		int[] a=new int[n+1];
		for(int i=1;i<=n;i++){
			a[i]=sc.nextInt();
		}
		int sum=0;
		int count=0;
		for(int i=1;i<=n;i++){
			sum+=a[i];
			if(sum>=k){
//				System.out.print(sum+" ");
				count++;
				sum=0;
				
			}else{
				if(i==n){
					count++;
				}
			}

		}
		System.out.println(count);

	}

}

 

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108346961