CSP201703-1: cake, csp201703-1 cake

Source: Internet
Author: User

CSP201703-1: cake, csp201703-1 cake

Introduction:CSP (http://www.cspro.org/lead/application/ccf/login.jsp) is a "Computer vocational qualification certification" examination initiated by CCF, perform capability certification for professionals in computer software development, software testing, information management, and other fields. The subjects of certification are those who are engaged in or will be engaged in IT professional technical and technical management personnel, as well as review subjects for university recruitment and postgraduate students.

 

  • Problem description

James has n cakes for his friends on his birthday. The weights of these n cakes are a1, a2 ,..., An. Xiaoming wants to give each friend a cake with at least k weight. James's friends have already lined up to get the cake. For every friend, James always gives the cake with the smallest number in his hand to him. When the weight of the cake is less than k, continue to give him the smallest number of the remaining cakes until James's cake is fully divided or the total weight of the cake from this friend is greater than or equal to k.

When James's cake was completed, how many friends had it.

  • Input Format

The first line of the input contains two integers n, k, meaning as described above.

The second row contains n positive integers, indicating a1, a2 ,..., An.

  • Output Format

Output an integer to indicate the number of friends who have sent the cake.

  • Sample Input

6 9

2 6 5 6 3 5

  • Sample output

3

  • Example

The first friend got the first three cakes, the second friend got 4th and five, and the third friend got the last one.

  • Scale and conventions of evaluation cases

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

 

  • Source code

# Include <stdio. h>

# Include <stdlib. h>

# Include <memory. h>

 

Int main (void)

{

Int n; // n pieces of cake

Int k; // weight: k

Int people = 0;

Scanf ("% d", & n );

Scanf ("% d", & k );

Int * input = (int *) malloc (sizeof (int) * n );

Memset (input, 0, sizeof (int) * n );

For (int I = 0; I <n; I ++)

{

Scanf ("% d", input + I );

}

Int weight, I, j;

For (I = 0; I <n ;)

{

Weight = 0;

For (j = I; j <n; j ++)

{

Weight + = input [j];

If (weight> = k)

{

People + = 1;

Break;

}

}

I = j + 1;

}

// The total weight of the remaining cake is less than k, and all of them are allocated to a friend.

If (weight <k)

{

People + = 1;

}

Printf ("% d \ n", people );

Free (input );

Return 0;

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.