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.
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.
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 an integer to indicate the number of friends who have sent the cake.
6 9
2 6 5 6 3 5
3
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 ≤.
# 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; } |