整型数字的英文表达和中文表达
有符号32位整数的英文表达和中文表达
【题目】
给定一个整型数字(32位)num,写两个函数分别返回num的英文与中文表达字符串。
num=0
英文表达字符串为:Zero
中文表达字符串为:零
num=319
英文表达字符串为:Three Hundred Nineteen
中文表达字符串为:三百一十九
num=1014
英文表达字符串为:One Thousand, Fourteen
中文表达字符串为:一千零十四
num=-2147483648
英文表达字符串为:Negative, Two Billion, One Hundred Forty Seven Million, Four Hundred Eighty Three Thousand, Six Hundred Forty Eight
中文表达字符串为:负二十一亿四千七百四十八万三千六百四十八
算法思路
查看业务逻辑能力,将阿拉伯数字以英文和中文表达显示。
32位整数范围为
−
2
31
∼
2
31
−
1
-2^{31} \sim 2^{31} - 1
−231∼231−1
英文表达以三个数字为一组,依次为Billion, Million, Thousand,三个数字内对应
1
∼
999
1 \sim 999
1∼999,1~19,20,30,40,50,60,70,80,90有特定英文单词,其余由前面特定的英文单词拼接;
中文表达以四个数字为一组,依次为亿, 万,四个数字内对应
1
∼
9999
1 \sim 9999
1∼9999,依次为千,百,十,个位为一、二、三、四、五、六、七、八、九,为每个单位的数值表示;
注意:
对0的特殊处理;
负数统一转换为正数处理,前面加负数表示;
java中用int
表示32位整数,
−
2
31
-2^{31}
−231绝对值会超出数的表示范围,而python是动态语言,不存在这个问题。我实现时逻辑模拟java解决这个问题,以数字的英文表达为例。
# 逻辑上规避最小值-2**31,有关数据的存储空间及表达范围
if num == -2**31:
res += "Two Billion, "
num %= -2 * 10 ** 9
相应代码
# 英文表达1~19
def eng_express_1_to_19(num):
if num < 1 or num > 19:
return ""
eng_numbers = ["One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ",
"Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
"Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "]
return eng_numbers[num - 1]
# 英文表达1~99
def eng_express_1_to_99(num):
if num < 1 or num > 99:
return ""
if num < 20:
return eng_express_1_to_19(num)
else:
eng_numbers = ["Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "]
ten_coef = num // 10
one_coef = num % 10
res = eng_numbers[ten_coef - 2]
if num != 0:
res += eng_express_1_to_19(one_coef)
return res
# 英文表达1~999
def eng_express_1_to_999(num):
if num < 1 or num > 999:
return ""
res = ""
hund_coef = num // 100
if hund_coef != 0:
res += eng_express_1_to_19(hund_coef) + "Hundred "
res += eng_express_1_to_99(num % 100)
return res
# 英文表达32位整数
def eng_express_num(num):
# 32位整数范围外
if num >= 2 ** 31 or num < - 2 ** 31:
return ""
if num == 0:
return "Zero"
res = ""
if num < 0:
res += "Negative, "
# 逻辑上规避最小值-2**31,有关数据的存储空间及表达范围
if num == -2**31:
res += "Two Billion, "
num %= -2 * 10 ** 9
num = abs(num)
print(num)
bill_coef = num // (10 ** 9)
num = num % 10 ** 9
if bill_coef != 0:
res += eng_express_1_to_999(bill_coef) + "Billion, "
mill_coef = num // (10 ** 6)
num = num % 10 ** 6
if mill_coef != 0:
res += eng_express_1_to_999(mill_coef) + "Million, "
thou_coef = num // (10 ** 3)
num = num % 10 ** 3
if thou_coef != 0:
res += eng_express_1_to_999(thou_coef) + "Thousand, "
res += eng_express_1_to_999(num)
return res
def chi_express_1_to_9(num):
if num < 1 or num > 9:
return ""
chi_numbers = ['一', '二', '三', '四', '五', '六', '七', '八', '九']
return chi_numbers[num - 1]
def chi_express_1_to_9999(num):
if num < 1 or num > 9999:
return ""
res = ""
thou_coef = num // 1000
num = num % 1000
if thou_coef != 0:
res += chi_express_1_to_9(thou_coef) + "千"
hund_coef = num // 100
num = num % 100
if hund_coef != 0:
res += chi_express_1_to_9(hund_coef) + "百"
ten_coef = num // 10
num = num % 10
if ten_coef != 0:
if thou_coef !=0 and hund_coef == 0:
res += "零"
res += chi_express_1_to_9(ten_coef) + "十"
res += chi_express_1_to_9(num)
return res
def chi_express_num(num):
if num < -2 ** 31 or num > 2 ** 31 - 1:
return ""
if num == 0:
return "零"
res = ""
if num < 0:
res += "负"
# 逻辑上规避最小值-2**31,有关数据的存储空间及表达范围
if num == -2**31:
res += "二十一亿"
num %= -10 ** 8
num = abs(num)
yi_coef = num // (10 ** 8)
num = num % (10 ** 8)
if yi_coef != 0:
res += chi_express_1_to_9999(yi_coef) + "亿"
wang_coef = num // (10 ** 4)
num = num % (10 ** 4)
if wang_coef != 0:
res += chi_express_1_to_9999(wang_coef) + "万"
res += chi_express_1_to_9999(num)
return res
# 简单测试
if __name__ == '__main__':
num = 0
print(eng_express_num(num)) # Zero
print(chi_express_num(num)) # 零
num = 319
print(eng_express_num(num)) # Three Hundred Nineteen
print(chi_express_num(num)) # 三百一十九
num = 1014
print(eng_express_num(num)) # One Thousand, Fourteen
print(chi_express_num(num)) # 一千零十四
num = -2147483648
print(eng_express_num(num)) # Negative, Two Billion, One Hundred Forty Seven Million, Four Hundred Eighty Three Thousand, Six Hundred Forty Eight
print(chi_express_num(num)) # 负二十一亿四千七百四十八万三千六百四十八
num = 2147483647
print(eng_express_num(num)) # Two Billion, One Hundred Forty Seven Million, Four Hundred Eighty Three Thousand, Six Hundred Forty Seven
print(chi_express_num(num)) # 二十一亿四千七百四十八万三千六百四十七
有任何疑问和建议,欢迎在评论区留言和指正!
感谢您所花费的时间与精力!