2015
1
def four2ten():
four = input("4進数を入力してください")
for i in four:
if not ( i == "0" or i == "1" or i == "2" or i == "3"):
return "4進数ではありません."
ten = 0
for i in range(len(four)):
ten += 4** (len(four) - (i + 1 ) ) * int(four[i])
return ten
print(four2ten())
27
2
def alpha2ten():
alpha = input("アルファベットを入力してください")
eight = ""
for i in alpha:
if not ( i == "a" or i == "b" or i == "c" or i == "d"or i == "e"or i == "f"or i == "g"or i == "h"):
return "8進数ではありません."
if i =="a":
eight += "0"
if i =="b":
eight += "1"
if i =="c":
eight += "2"
if i =="d":
eight += "3"
if i =="e":
eight += "4"
if i =="f":
eight += "5"
if i =="g":
eight += "6"
if i =="h":
eight += "7"
ten = 0
for i in range(len(eight)):
ten += 8 ** (len(eight) - (i + 1 ) ) * int(eight[i])
return ten
print(alpha2ten())
83
3
MMXV
4
気をつけんのは特殊ルールのみ
import re
def rome2ten():
rome = input("ローマ数字を入力してください")
for i in rome:
if not ( i == "I" or i == "V" or i == "X" or i == "L"or i == "C"or i == "D"or i == "M"):
return "ローマ数字ではありません."
# IV= q(4), IX = w(9), XL = e(40), XC = r(90), CD = t(400), CM = y(900)
rome = rome.replace("IV","q")
rome = rome.replace("IX","w")
rome = rome.replace("XL","r")
rome = rome.replace("XC","r")
rome = rome.replace("CD","t")
rome = rome.replace("CM","y")
ten = 0
for i in rome:
if i == "I":
ten += 1
if i == "V":
ten += 5
if i == "X":
ten += 10
if i == "L":
ten += 50
if i == "C":
ten += 100
if i == "D":
ten += 500
if i == "M":
ten += 1000
if i == "q":
ten += 4
if i == "w":
ten += 9
if i == "e":
ten += 40
if i == "r":
ten += 90
if i == "t":
ten += 400
if i == "y":
ten += 900
return ten
print(rome2ten())
2009
5
気を使うのは各位の値が4 or 9のとき
def ten2rome():
ten = input()
rome = ""
10 ** (len(ten) - 1)
if (len(ten) == 4):
rome += int(ten[0])*"M"
if ten[1] == "4":
rome += "CD"
if ten[1] == "9":
rome += "CM"
if 9 > int(ten[1]) > 4:
rome += "D" + (int(ten[1]) - 5) * "C"
if 0 <= int(ten[1]) < 5:
rome += int(ten[1]) * "C"
if ten[2] == "4":
rome += "XL"
if ten[2] == "9":
rome += "XC"
if 9 > int(ten[2]) > 4:
rome += "C" + (int(ten[2]) - 5) * "X"
if 0 <= int(ten[2]) < 5:
rome += int(ten[2]) * "X"
if ten[3] == "4":
rome += "IV"
if ten[3] == "9":
rome += "IX"
if 9 > int(ten[3]) > 4:
rome += "V" + (int(ten[3]) - 5) * "I"
if 0 <= int(ten[3]) < 5:
rome += int(ten[3]) * "I"
if (len(ten) == 3):
if ten[0] == "4":
rome += "CD"
if ten[0] == "9":
rome += "CM"
if 9 > int(ten[0]) > 4:
rome += "D" + (int(ten[0]) - 5) * "C"
if 0 <= int(ten[0]) < 5:
rome += int(ten[0]) * "C"
if ten[1] == "4":
rome += "XL"
if ten[1] == "9":
rome += "XC"
if 9 > int(ten[1]) > 4:
rome += "C" + (int(ten[1]) - 5) * "X"
if 0 <= int(ten[1]) < 5:
rome += int(ten[1]) * "X"
if ten[2] == "4":
rome += "IV"
if ten[2] == "9":
rome += "IX"
if 9 > int(ten[2]) > 4:
rome += "V" + (int(ten[2]) - 5) * "I"
if 0 <= int(ten[2]) < 5:
rome += int(ten[2]) * "I"
if (len(ten) == 2):
if ten[0] == "4":
rome += "XL"
if ten[0] == "9":
rome += "XC"
if 9 > int(ten[0]) > 4:
rome += "C" + (int(ten[0]) - 5) * "X"
if 0 <= int(ten[0]) < 5:
rome += int(ten[0]) * "X"
if ten[1] == "4":
rome += "IV"
if ten[1] == "9":
rome += "IX"
if 9 > int(ten[1]) > 4:
rome += "V" + (int(ten[1]) - 5) * "I"
if 0 <= int(ten[1]) < 5:
rome += int(ten[1]) * "I"
if (len(ten) == 1):
if ten[0] == "4":
rome += "IV"
if ten[0] == "9":
rome += "IX"
if 9 > int(ten[0]) > 4:
rome += "V" + (int(ten[0]) - 5) * "I"
if 0 <= int(ten[1]) < 5:
rome += int(ten[0]) * "I"
return rome
print(ten2rome())
MMIX