#31. 获取数组前两元素
获取数组前两元素
Get First Two Elements
Background
Congcong is currently learning about array operations. He encountered a task that requires extracting a specific number of elements from a given array.
Problem Description
Given an integer array of any length, return a new array containing its first elements. If the array is smaller than length , use whatever elements are present.
Input Format
Input is given from Standard Input in the following format.
The input consists of a single line representing an integer array. Array elements are space-separated and enclosed in square brackets
[].
Output Format
Output is to Standard Output in the following format.
The output consists of a single line representing a new integer array. Array elements are separated by commas and spaces, and enclosed in square brackets
[].
Sample
[1 2 3]
[1, 2]
[1 2]
[1, 2]
[1]
[1]
Sample Explanation
- Sample 1: The original array is
[1 2 3]. Its length is greater than , so the first two elements[1, 2]are returned. - Sample 2: The original array is
[1 2]. Its length is equal to , so the first two elements[1, 2]are returned. - Sample 3: The original array is
[1]. Its length is less than , so all existing elements[1]are returned.
Constraints
Time limit: second, Memory limit: KiB for each test case.