1.用传统方法计算一个数的二进制
1.只能计算小于2**16的数字
1 i = 17 2 num_2 = int(input("输入一个数字:").strip()) 3 while True: 4 i -= 1 5 if i < 0: 6 break 7 if 2 ** i > num_2: 8 print(0,end="") 9 else: 10 print(1,end="") 11 num_2 = num_2 - 2 ** i
2.用平常数学方法解
num_2 = int(input("输入一个数字:").strip()) l = [] while True: b = num_2 // 2 a = num_2 % 2 num_2 = b l.append(a) if b == 0: break l.reverse() for i in l: print(i,end="")
2.用特殊字符输出一个指定高和底的空心长方形
1 gao = int(input("输入高:").strip()) 2 di = int(input("输入底").strip()) 3 count1 = 1 4 while count1 <= gao: 5 count2 = 0 6 while count2 <= di: 7 count2 += 1 8 if 2 <= count1 < gao and 2 <= count2 <= di: 9 print(" ",end="") 10 continue 11 print("#",end="") 12 print() 13 count1 +=1
结果:
3.用特殊字符实现LED灯效果
1 1 a = " ********" 2 2 b = " * " 3 3 c = " *" 4 4 d = " * *" 5 5 num = [ 6 6 [a,d,d,d,d,d,a], 7 7 [c,c,c,c,c,c,c], 8 8 [a,c,c,a,b,b,a], 9 9 [a,c,c,a,c,c,a], 10 10 [d,d,d,a,c,c,c], 11 11 [a,b,b,a,c,c,a], 12 12 [a,b,b,a,d,d,a], 13 13 [a,c,c,c,c,c,c], 14 14 [a,d,d,a,d,d,a], 15 15 [a,d,d,a,c,c,c] 16 16 ] 17 17 int1 = input("输入数字:") 18 18 for i in range(7): 19 19 for j in int1: 20 20 print(num[int(j)][i], end=" ") 21 21 print()
结果:
时间: 2024-12-21 14:59:07