CSP201509-1: array segmentation, csp201509-1 Segmentation
Introduction:CSPHttp://www.cspro.org/lead/application/ccf/login.jsp)YesbyChina Computer Society (CCFThe "Computer vocational qualification certification" examination is initiated to authenticate the competence of professionals in computer software development, software testing, information management, and other fields. The authenticated object is engaged in or will be engaged inITSpecialized technical and technical management personnel in the field, as well as review objects for university candidates.
LProblem description
Given an integer series, the same consecutive longest integer series in the series is counted as one segment. How many segments are there in the series?
LInput Format
The first line of the input contains an integer n, indicating the number of integers in the series.
The second row contains n integers a1, a2 ,..., An, indicating the given series. Adjacent integers are separated by a space.
LOutput Format
Output an integer that indicates that the given series has multiple segments.
LSample Input
8
8 8 8 0 12 8 0
LSample output
5
LExample
8 8 8 is the first segment, 0 is the second segment, 12 12 is the third segment, the second to last integer 8 is the fourth segment, and the last 0 is the fifth segment.
LScale and conventions of evaluation cases
1 ≤ n ≤ 1000, 0 ≤ ai ≤.
LSource code
# Include <stdio. h> # Include <stdlib. h> # Include <memory. h> Int main (void) { Int n; // number Int result = 0; Int currentNum =-1; Scanf ("% d", & n ); Int * input = (int *) malloc (sizeof (int) * n ); Memset (input, 0, sizeof (int) * n ); For (int I = 0; I <n; I ++) { Scanf ("% d", input + I ); } For (int I = 0; I <n; I ++) { If (currentNum! = Input [I]) { CurrentNum = input [I]; Result + = 1; } } Printf ("% d \ n", result ); Free (input ); Return 0; } |