#78. 判断数组元素是否仅含1和4
判断数组元素是否仅含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
trueif every element in the array is either 1 or 4; otherwise, outputfalse.
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 istrue. - Sample 2: The array
[1, 4, 2, 4]contains the element 2, which is neither 1 nor 4, so the output isfalse. - Sample 3: All elements in the array
[1, 1]are either 1 or 4, so the output istrue.
Constraints
For all test cases, the length of the input string does not exceed 1000. Each element in the array satisfies .