#78. 判断数组元素是否仅含1和4

    ID: 78 Type: Default 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>codingbatArray-2gesp3字符串一维数组

判断数组元素是否仅含1和4

Contains Only 1s and 4s

Background

Dawei is studying array properties. He wants to know if an array contains only specific numbers.

Problem Description

Given an array of integers, return true if every element in the array is either 1 or 4.

Input Format

Input is given from standard input in the following format.

The input consists of a single line string, representing an array of integers. The string starts with a square bracket [ and ends with ], containing several integers separated by spaces.

Output Format

Output is printed to standard output in the following format.

Output true if every element in the array is either 1 or 4; otherwise, output false.

Sample

[1 4 1 4]
true
[1 4 2 4]
false
[1 1]
true

Sample Explanation

  • Sample 1: All elements in the array [1, 4, 1, 4] are either 1 or 4, so the output is true.
  • Sample 2: The array [1, 4, 2, 4] contains the element 2, which is neither 1 nor 4, so the output is false.
  • Sample 3: All elements in the array [1, 1] are either 1 or 4, so the output is true.

Constraints

For all test cases, the length of the input string does not exceed 1000. Each element xx in the array satisfies 0x10000 \le x \le 1000.