【题目】
给定一个32位整数n,写两个函数分别返回n的英文与中文表达字符串。
【基本思路】
该类型的题目通常是由小的,简单的场景出发,把复杂的事情拆解成简单的场景。
英文表达的实现。英文的表达是以三个数为一组的,所以只要能表达出1~999,然后将数字分解成十亿组,百万组,千组,1~999组,每组都用1~999表达再把组与组之间各自的表达字符串连接起来即可。如何实现1~999的表达?从简单场景出发,依次实现1~19,1~99,1~999即可。
需要注意的是负数要加一个前缀,n如果为最小负整数的时候要考虑取绝对值时会溢出的问题。
#python3.5
def num1To19(num):
if num < 1 or num > 19:
return ""
names = ["One","Two","Three","Four","Five","Six","Seven","Eight","Nine",\
"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen",\
"Sixteen","Seventeen","Eighteen","Nineteen"]
return names[num-1]
def num1To99(num):
if num < 1 or num > 99:
return ""
if num >= 1 and num <= 19:
return num1To19(num)
high = num // 10
tyNames = ["Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"]
return tyNames[high-2] + " " + num1To19(num % 10)
def num1To999(num):
if num < 1 or num > 999:
return ""
if num >= 1 and num <= 99:
return num1To99(num)
return num1To19(num // 100) + " Hundred " + num1To99(num % 100)
def getNumEngExp(num):
if num == 0:
return "Zero"
res = ""
if num < 0:
res = "Negetive, "
if num == -(1 << 31): #防止取绝对值时溢出
res += "Two Billion, "
num %= -2000000000
num = abs(num)
high = 1000000000
names = ["Billion","Million","Thousand",""]
index = 0
while num != 0:
cur = num // high
num %= high
if cur != 0:
res += num1To999(cur) + " " + names[index]
res += ", " if num != 0 else ""
index += 1
high //= 1000
return res
中文表达的实现。与英文表达的处理过程类似,都是由小范围的数向大范围的数扩张。依次实现1~9,1~99,1~999,1~9999,1~99999999。需要注意的细节就是,16的表达为十六,116的表达为一百一十六,1016的表达是一千零一十六,也可以是一千零十六。这说明,对10~19来说,如果百位上有数字,那么就应该念一十几,如果百位没有数字,则应该念十几,所以在设计1~99函数的时候,要多考虑百位是否存在的因素。
def num1To9ByChinese(num):
if num < 1 or num > 9:
return ""
names = ["一","二","三","四","五","六","七","八","九"]
return names[num-1]
def num1To99ByChinese(num, hasBai):
if num < 1 or num > 99:
return ""
if num < 10:
return num1To9ByChinese(num)
shi = num // 10
if shi == 1 and not hasBai:
return "十" + num1To9ByChinese(num % 10)
else:
return num1To9ByChinese(shi) + "十" + num1To9ByChinese(num % 10)
def num1To999ByChinese(num):
if num < 1 or num > 999:
return ""
if num < 100:
return num1To99ByChinese(num, False)
res = num1To9ByChinese(num // 100) + "百"
rest = num % 100
if rest == 0:
return res
elif rest >= 10:
res += num1To99ByChinese(rest, True)
else:
res += "零" + num1To9ByChinese(rest)
return res
def num1To9999ByChinese(num):
if num < 1 or num > 9999:
return ""
if num < 1000:
return num1To999ByChinese(num)
res = num1To9ByChinese(num // 1000) + "千"
rest = num % 1000
if rest == 0:
return res
elif rest >= 100:
res += num1To999ByChinese(rest)
else:
res += "零" + num1To99ByChinese(rest, False)
return res
def num1To99999999ByChinese(num):
if num < 1 or num > 99999999:
return ""
if num < 10000:
return num1To9999ByChinese(num)
wan = num // 10000
rest = num % 10000
res = num1To9999ByChinese(wan) + "万"
if rest == 0:
return res
elif rest > 1000:
res += num1To9999ByChinese(rest)
else:
res += "零" + num1To999ByChinese(rest)
return res
def getNumChineseExp(num):
if num == 0:
return "零"
res = ""
if num < 0:
res = "负"
num = abs(num)
yi = num // 100000000
rest = num % 100000000
if yi == 0:
return res + num1To99999999ByChinese(rest)
res += num1To99999999ByChinese(yi) + "亿"
if rest == 0:
return res
elif rest > 10000000:
return res + num1To99999999ByChinese(rest)
else:
return res + "零" + num1To99999999ByChinese(rest)