python實(shí)現(xiàn)進(jìn)制轉(zhuǎn)化的示例代碼
做題思路
(1)掌握十進(jìn)制轉(zhuǎn)化為其他進(jìn)制的方法 (2)分析和解決如何將整數(shù)和小數(shù)分離,以及他們的存儲方式。(3)如何設(shè)計(jì)python函數(shù)去實(shí)現(xiàn)它們 (4)了解輾轉(zhuǎn)相除法和列表如何運(yùn)用
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、運(yùn)用的知識點(diǎn)
(1)鍵盤輸入函數(shù),列表和類型轉(zhuǎn)化 (2)運(yùn)算符的理解(/ 和 //的區(qū)別) (3) 輾轉(zhuǎn)相除法求余數(shù) (4)棧和隊(duì)列的知識
二、例題展示
1.題目
代碼如下(示例):
輸入一個自然數(shù),輸出它的二進(jìn)制、八進(jìn)制、十六進(jìn)制表示形式
2.代碼
代碼如下(示例):
# 自然數(shù) = 十進(jìn)制數(shù) print("----------------------") print("------ 0.小數(shù) -------") print("------ 1.整數(shù) -------") flag = int(input()) # 初始化a,b的值 a = 0 b = 0.0 x = "" # 判斷是否為小數(shù)或者整數(shù) if flag == 0: x = input("請任意輸入一個小數(shù):") # a為整數(shù)部分,b為小數(shù)部分 a = int(float(x)) b = float(x) - a elif flag == 1: x = input("請任意輸入一個整數(shù):") a = int(x) else: print("輸入格式錯誤") """ 三者都是用輾轉(zhuǎn)相除法求解 """ list_en = ['A','B','C','D','E','F'] list_1 = [] # 收集整數(shù)部分 list_2 = [] # 收集小數(shù)部分 list_3 = [] # 收集整數(shù)部分 list_4 = [] # 收集小數(shù)部分 list_5 = [] # 收集整數(shù)部分 list_6 = [] # 收集小數(shù)部分 # 求二進(jìn)制數(shù) def fun_two(n,m): if m == 0.0: #整數(shù)部分 while n != 0: i = n % 2 list_1.insert(0,i) # 類似于壓棧 n = n // 2 else : count = 0 while n != 0: i = n % 2 list_1.insert(0,i) # 類似于壓棧 n = n // 2 while count != 10: m = m * 2 j = int(m) m = m - j list_2.append(j) count = count + 1 # 求八進(jìn)制數(shù) def fun_eight(n, m): if m == 0.0: # 整數(shù)部分 while n != 0: i = n % 8 list_3.insert(0, i) # 類似于壓棧 n = n // 8 else: count = 0 while n != 0: i = n % 8 list_3.insert(0, i) # 類似于壓棧 n = n // 8 while count != 10: m = m * 8 j = int(m) m = m - j list_4.append(j) count = count + 1 # 求十六進(jìn)制數(shù) def fun_sixteen(n, m): if m == 0.0: # 整數(shù)部分 while n != 0: i = n % 16 list_5.insert(0, i) # 類似于壓棧 n = n // 16 else: count = 0 while n != 0: i = n % 16 list_5.insert(0, i) # 類似于壓棧 n = n // 16 while count != 10: m = m * 16 j = int(m) m = m - j list_6.append(j) count = count + 1 print("yes") def print_str(list1): s = "" for item in list1: if item == '.': item = '.' elif int(item) > 10: l = item - 10 item = list_en[l] s = s + str(item) return s # 二進(jìn)制求解 fun_two(a,b) # 八進(jìn)制求解 fun_eight(a,b) # 十六進(jìn)制求解 fun_sixteen(a,b) list_1.append('.') list_1.extend(list_2) list_3.append('.') list_3.extend(list_4) list_5.append('.') list_5.extend(list_6) s_two = print_str(list_1) s_eight = print_str(list_3) s_sixteen = print_str(list_5) # 測試結(jié)果 if __name__ == '__main__': print(a) print(b) print(type(a)) print(type(b)) print(list_1) print(list_3) print(list_5) print("二進(jìn)制值:", end = "") print(s_two) print("八進(jìn)制值:", end="") print(s_eight) print("十六進(jìn)制值:", end="") print(s_sixteen)
總結(jié)
結(jié)果展示:
到此這篇關(guān)于python實(shí)現(xiàn)進(jìn)制轉(zhuǎn)化的示例代碼的文章就介紹到這了,更多相關(guān)python 進(jìn)制轉(zhuǎn)化內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。