四折数(有时称为四向数)是在前后翻转,上下镜像或上下翻转时保持不变的数字。
换句话说,四序数是回文数,仅包含数字中的0、1和8。
前几个四折数是:
0, 1, 8, 11, 88, 101, 111, 181, 808, 818, 888, 1001, 1111, 1881, ….
检查N是否为四位数
给定数字N ,任务是检查N是否是四位数。
例子:
Input: N = 101
Output: Yes
Explanation:
101 is palindrome number and it contains only 0, 1 and 8 as digits in the number.
Input: N = 1221
Output: No
方法:想法是检查数字是否是回文。如果数字是回文,请检查数字。如果所有数字都位于集合(0,1,8)中,则该数字为四分位数。
例如:
For N = 101
// As N is a palindromic number
// Alse the digits in the number is
// from the set {0, 1, 8}
// Therefore, N is a Tetradic number
下面是上述方法的实现:
Python3
# Python3 implementation for
# the above approach
# Function to check if the number
# N having all digits lies in
# the set (0, 1, 8)
def isContaindigit(n):
temp = str(n)
for i in temp:
if i not in ['0', '1', '8']:
return False
return True
# Function to check if the number
# N is palindrome
def ispalindrome(n):
temp = str(n)
if temp == temp[::-1]:
return True
return False
# Function to check if a number
# N is Tetradic
def isTetradic(n):
if ispalindrome(n):
if isContaindigit(n):
return True
return False
# Driver Code
N = 101
# Function Call
if(isTetradic(N)):
print("Yes")
else:
print("No")
输出:
Yes