在python中,为什么我对2位数的mod(%)的操作返回零
作为一个完全的初学者,我一直在尝试一些leetcode问题,有一节要求我转换/返回数字的千、百、十和单位位置 这是python中的代码在python中,为什么我对2位数的mod(%)的操作返回零,python,mod,Python,Mod,作为一个完全的初学者,我一直在尝试一些leetcode问题,有一节要求我转换/返回数字的千、百、十和单位位置 这是python中的代码 class Solution: def intToRoman(self, num: int) -> str: list = {1 : 'I', 4 : 'IV', 5 : 'V', 9 : 'IX',
class Solution:
def intToRoman(self, num: int) -> str:
list = {1 : 'I',
4 : 'IV',
5 : 'V',
9 : 'IX',
10: 'X',
40: 'XL',
50: 'L',
90: 'XC',
100: 'C',
400: 'CD',
500: 'D',
900: 'CM',
1000: 'M'}
thousands, hundreds, tens, unit = 0,0,0,0
string = ""
number = int(num)
#A Thousandth number
if number >= 1000:
thousands = number - (number % 1000) # 3549 - (3549 % 1000) = 3000
hundreds = number - thousands; #// 3549 - 3000 = 549
tens = hundreds % 100 #// 549 % 100 = 49
hundreds = hundreds - (hundreds % 100) #// 549 - (549 % 100) = 500
units = tens % 10
tens = tens - (tens % 10) #// 49 - (49 % 10) = 49 - 9 = 9
thou = thousands // 1000 # 3
#THOUSANDS
for i in range(thou):
string = string + (list[1000])
#HUNDREDS
hund = hundreds // 100
if hund < 4:
string = string +(hund * list[100])
elif hund == 4:
string = string +(list[400])
elif hund == 5:
string = string +(list[500])
elif hund >= 6 and hund <= 8:
string = string +(list[500])
hund = hund - 5
string = string +(hund * list[100])
elif hund == 9:
string = string +(list[900])
#TENS
ten = tens // 10
if ten < 4:
string = string +(ten * list[10])
elif ten == 4:
string = string +(list[40])
elif ten == 5:
string = string +(list[50])
elif ten >= 6 and ten <= 8:
string = string +(list[50])
ten = ten - 5
string = string +(ten * list[10])
elif ten == 9:
string = string +(list[90])
#UNITS
if unit < 4:
string = string +(unit * list[1])
elif unit == 5:
string = string +(list[5])
elif unit >= 6 and unit <= 8:
string = string +(list[5])
unit = unit - 5
string = string +(unit * list[1])
elif unit == 9:
string = string +(list[9])
return string
现在,我的问题是Units变量保持为零。我不明白为什么?这段代码在翻译成C时可以正常工作。python中的Mod有什么我不知道的地方吗
例如:如果输入的数字是3549整数,则必须将其转换为罗马数字,units变量保持零。输出应该是MMMDXLIX
由于输出中的IX取决于因某种原因保持零的units变量,因此输出为MMMDXL
顺便说一句,该函数仅适用于大于等于1000的数字。我没有进一步编码。这些代码块中有一个输入错误:
if number >= 1000:
thousands = number - (number % 1000) # 3549 - (3549 % 1000) = 3000
hundreds = number - thousands; #// 3549 - 3000 = 549
tens = hundreds % 100 #// 549 % 100 = 49
hundreds = hundreds - (hundreds % 100) #// 549 - (549 % 100) = 500
unit = tens % 10 # you input "units" here
tens = tens - (tens % 10) #// 49 - (49 % 10) = 49 - 9 = 9
thou = thousands // 1000 # 3
结果:
MMMDXLIX
顺便说一句,这个问题得到了更好的解决方案
我的代码:
def intToRoman(num: int) -> str:
roman_dict_key = [1,4,5,9,10,40,50,90,100,400,500,900,1000][::-1]
roman_dict_value = ["I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"][::-1]
roman_dict = dict(zip(roman_dict_key,roman_dict_value))
ans = ""
for digit,roman in roman_dict.items():
while num>=digit:
num -= digit
ans += roman
return ans
请修正你的缩进。此外,我还不清楚您是使用num还是no。请提供一个。如果输入的数字是3549,您的代码单元将返回9。我已经编辑了这篇文章。很抱歉搞得一团糟,我在C和Python之间进行了复制粘贴,忘记了跟踪变量和缩进@OrbitalTry我编辑的整个函数,@leaf_Yakitorio当然,就是这么简单。我在这上面花了太多时间,结果错过了这么简单的东西。非常感谢@Daniel Chettiar,我建议你使用IDE来编写代码,比如VSCODE,pyCARM.IDE会告诉你是否有一个打印错误。考虑用Python方式反转列表,如:::(1)。