#188. 提取数组中前N个偶数

    ID: 188 Type: Default 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>codingbatAp-1gesp1循环结构条件结构

提取数组中前N个偶数

Extract First N Even Numbers

Background

Congcong is learning array operations and has encountered a task that requires filtering specific elements from an integer array.

Problem Description

Given an array of positive integers, and an integer countcount. Return a new array of length countcount containing the first countcount even numbers from the original array. It is guaranteed that the original array will contain at least countcount even numbers.

Input Format

Input is given from standard input in the following format.

A single line containing several positive integers. The first part represents the elements of array arrarr (space-separated), and the last integer represents countcount.

Output Format

Output is printed to standard output in the following format.

A single line representing the new array, containing the first countcount even numbers from the original array. Array elements are separated by commas and spaces, and enclosed in square brackets [].

Sample

3 2 4 5 8 2
[2, 4]
3 2 4 5 8 3
[2, 4, 8]
6 1 2 4 5 8 3
[6, 2, 4]

Sample Explanation

Sample 1: Input array [3 2 4 5 8] and count=2count = 2. The even numbers in the original array are 2,4,82, 4, 8 in order. Taking the first 22 even numbers yields [2, 4]. Sample 2: Input array [3 2 4 5 8] and count=3count = 3. The even numbers in the original array are 2,4,82, 4, 8 in order. Taking the first 33 even numbers yields [2, 4, 8]. Sample 3: Input array [6 1 2 4 5 8] and count=3count = 3. The even numbers in the original array are 6,2,4,86, 2, 4, 8 in order. Taking the first 33 even numbers yields [6, 2, 4].

Constraints

Time limit: 1s, Memory limit: 1024KB.