十进制转化二进制
while 1:print('仅支持100以下换算')
variable = input('请输入10进制数字:')
# 用户输入一个数字 2
before = int(variable)
# 讲用户输入数字转化整型
after = before // 2
if before == 2:
afters = before % 2
print(after,afters)
else:
afters = before % 2
if before == 1:
print('1')
else:
if before == 0:
print('0')
else:
if before < 0:
print('请输入一个比0大的数字!')
if before > 100:
print('请输入100以下的数字!')
else:
tis = after // 2
tiss = after % 2
if after == 1:
tiss = 1
print(tiss,afters)
else:
tiss = after % 2
pis = tis // 2
piss = tis % 2
if tis == 1:
piss = 1
print(piss,tiss,afters)
else:
piss = tis % 2
tiis = pis // 2
tiiss = pis % 2
if pis == 1:
tiiss = 1
print(tiiss,piss,tiss,afters)
else:
tiiss = pis % 2
wins = tiis // 2
winss = tiis % 2
if tiis == 1:
winss = 1
print(winss,tiiss,piss,tiss,afters)
else:
winss = tiis % 2
pos = wins // 2
poss = wins % 2
if wins == 1:
poss = 1
print(poss,winss,tiiss,piss,tiss,afters)
else:
poss = wins % 2
wies = pos // 2
wiess = pos % 2
if pos == 1:
wiess = 1
print(wiess,poss,winss,tiiss,tiss,piss,afters)
else:
wiess = pos % 2
print(wiess,poss,winss,tiiss,piss,tiss,afters)
其他的我试了可以 就 100 和98 不行暂时是这样的 有什么地方不对 各位大佬 呵呵,代码不是这样写的哦 你可以参考一下这里
https://blog.csdn.net/yzmumu/article/details/80775784 本帖最后由 傻眼貓咪 于 2021-9-4 12:54 编辑
def binary(num: int) -> int:
n = num
res = ''
while n > 1:
if n%2 == 0:
res = res+'0'
n = int(n/2)
else:
res = res+'1'
n = int((n-1)/2)
res = int((res+'1')[::-1])
return res
print(binary(42)) # 101010 还是用bin()吧{:10_256:} suchocolate 发表于 2021-9-4 13:44
还是用bin()吧
是很方便沒有錯,但是這樣就失去了題目的目的性了 傻眼貓咪 发表于 2021-9-4 14:10
是很方便沒有錯,但是這樣就失去了題目的目的性了
那你可以这样{:10_256:}:d = int(input('Please in put a decimal number:'))
if d == 1:
print(1)
elif d == 2:
print(10)
elif d == 3:
print(11)
elif d == 4:
print(100)
elif d == 5:
print(101)
elif d == 6:
print(110)
elif d == 7:
print(111)
elif d == 8:
print(1000)
elif d == 9:
print(1001)
elif d == 10:
print(1010)
elif d == 11:
print(1011)
elif d == 12:
print(1100)
elif d == 13:
print(1101)
elif d == 14:
print(1110)
elif d == 15:
print(1111)
elif d == 16:
print(10000)
elif d == 17:
print(10001)
elif d == 18:
print(10010)
elif d == 19:
print(10011)
elif d == 20:
print(10100)
elif d == 21:
print(10101)
elif d == 22:
print(10110)
elif d == 23:
print(10111)
elif d == 24:
print(11000)
elif d == 25:
print(11001)
elif d == 26:
print(11010)
elif d == 27:
print(11011)
elif d == 28:
print(11100)
elif d == 29:
print(11101)
elif d == 30:
print(11110)
elif d == 31:
print(11111)
elif d == 32:
print(100000)
elif d == 33:
print(100001)
elif d == 34:
print(100010)
elif d == 35:
print(100011)
elif d == 36:
print(100100)
elif d == 37:
print(100101)
elif d == 38:
print(100110)
elif d == 39:
print(100111)
elif d == 40:
print(101000)
elif d == 41:
print(101001)
elif d == 42:
print(101010)
elif d == 43:
print(101011)
elif d == 44:
print(101100)
elif d == 45:
print(101101)
elif d == 46:
print(101110)
elif d == 47:
print(101111)
elif d == 48:
print(110000)
elif d == 49:
print(110001)
elif d == 50:
print(110010)
elif d == 51:
print(110011)
elif d == 52:
print(110100)
elif d == 53:
print(110101)
elif d == 54:
print(110110)
elif d == 55:
print(110111)
elif d == 56:
print(111000)
elif d == 57:
print(111001)
elif d == 58:
print(111010)
elif d == 59:
print(111011)
elif d == 60:
print(111100)
elif d == 61:
print(111101)
elif d == 62:
print(111110)
elif d == 63:
print(111111)
elif d == 64:
print(1000000)
elif d == 65:
print(1000001)
elif d == 66:
print(1000010)
elif d == 67:
print(1000011)
elif d == 68:
print(1000100)
elif d == 69:
print(1000101)
elif d == 70:
print(1000110)
elif d == 71:
print(1000111)
elif d == 72:
print(1001000)
elif d == 73:
print(1001001)
elif d == 74:
print(1001010)
elif d == 75:
print(1001011)
elif d == 76:
print(1001100)
elif d == 77:
print(1001101)
elif d == 78:
print(1001110)
elif d == 79:
print(1001111)
elif d == 80:
print(1010000)
elif d == 81:
print(1010001)
elif d == 82:
print(1010010)
elif d == 83:
print(1010011)
elif d == 84:
print(1010100)
elif d == 85:
print(1010101)
elif d == 86:
print(1010110)
elif d == 87:
print(1010111)
elif d == 88:
print(1011000)
elif d == 89:
print(1011001)
elif d == 90:
print(1011010)
elif d == 91:
print(1011011)
elif d == 92:
print(1011100)
elif d == 93:
print(1011101)
elif d == 94:
print(1011110)
elif d == 95:
print(1011111)
elif d == 96:
print(1100000)
elif d == 97:
print(1100001)
elif d == 98:
print(1100010)
elif d == 99:
print(1100011)
else:
print(False)
suchocolate 发表于 2021-9-4 14:29
那你可以这样:
学废了,回头我就去弄个大点的显示器{:10_256:}.... #-*- coding: utf-8
from decimal import Decimal
def bTod(n, pre=4):
'''
把一个带小数的二进制数n转换成十进制
小数点后面保留pre位小数
'''
string_number1 = str(n) #number1 表示二进制数,number2表示十进制数
decimal = 0#小数部分化成二进制后的值
flag = False
for i in string_number1: #判断是否含小数部分
if i == '.':
flag = True
break
if flag: #若二进制数含有小数部分
string_integer, string_decimal = string_number1.split('.') #分离整数部分和小数部分
for i in range(len(string_decimal)):
decimal += 2**(-i-1)*int(string_decimal)#小数部分化成二进制
number2 = int(str(int(string_integer, 2))) + decimal
return round(number2, pre)
else: #若二进制数只有整数部分
return int(string_number1, 2)#若只有整数部分 直接一行代码二进制转十进制 python还是骚
def dTob(n, pre=4):
'''
把一个带小数的十进制数n转换成二进制
小数点后面保留pre位小数
'''
string_number1 = str(n) #number1 表示十进制数,number2表示二进制数
flag = False
for i in string_number1: #判断是否含小数部分
if i == '.':
flag = True
break
if flag:
string_integer, string_decimal = string_number1.split('.') #分离整数部分和小数部分
integer = int(string_integer)
decimal = Decimal(str(n)) - integer
l1 =
l2 = []
decimal_convert = ""
while True:
if integer == 0: break
x,y = divmod(integer, 2)#x为商,y为余数
l2.append(y)
integer = x
string_integer = ''.join(])#整数部分转换成二进制
i = 0
while decimal != 0 and i < pre:
result = int(decimal * 2)
decimal = decimal * 2 - result
decimal_convert = decimal_convert + str(result)
i = i + 1
string_number2 = string_integer + '.' + decimal_convert
return float(string_number2)
else: #若十进制只有整数部分
l1 =
l2 = []
while True:
if n == 0: break
x,y = divmod(n, 2)#x为商,y为余数
l2.append(y)
n = x
string_number = ''.join(])
return int(string_number)
举个例子:
>>>dTob(5.31,5)#将5.31转成二进制,保留5位小数
101.01001
>>>bTod(101.01001,5)#将101.01001转成十进制,保留5位小数
5.28125
浮点数运算可能有一点误差
页:
[1]