对于给定的长度n,找到所有n个长度的笔迹图编号。
频闪数字是一个数字,其数字是旋转对称的,因此旋转180度时它看起来是相同的。换句话说,“频闪图数”在右侧和上下都相同。
0 after 180° rotation : (0 → 0)
1 after 180° rotation : (1 → 1)
8 after 180° rotation : (8 → 8)
6 after 180° rotation : (6 → 9)
9 after 180° rotation : (9 → 6)
例子 :
Input : n = 2
Output : 88 11 96 69
Input : n = 4
Output : 8008 1001 9006 6009 8888 1881 9886 6889 8118 1111
9116 6119 8968 1961 9966 6969 8698 1691 9696 6699
以下是Python3的实现:
# Python program to print all
# Strobogrammatic number of length n
# strobogrammatic function
def strobogrammatic_num(n):
result = numdef(n, n)
return result
# definition function
def numdef(n, length):
if n == 0: return [""]
if n == 1: return ["1", "0", "8"]
middles = numdef(n - 2, length)
result = []
for middle in middles:
if n != length:
result.append("0" + middle + "0")
result.append("8" + middle + "8")
result.append("1" + middle + "1")
result.append("9" + middle + "6")
result.append("6" + middle + "9")
return result
# Driver Code
if __name__ == '__main__':
# Print all Strobogrammatic
# number for n = 3
print(strobogrammatic_num(3))
输出 :
['818', '111', '916', '619', '808', '101', '906', '609', '888', '181', '986', '689']
参考: https://en.wikipedia.org/wiki/Strobogrammatic_number