牛客[编程题] HJ42 学英语
较难 通过率:23.96% 时间限制:1秒 空间限制:32M
描述
Jessi初学英语,为了快速读出一串数字,编写程序将数字转换成英文:
具体规则如下:
1.在英语读法中三位数字看成一整体,后面再加一个计数单位。从最右边往左数,三位一单位,例如12,345 等
2.每三位数后记得带上计数单位 分别是thousand, million, billion.
3.公式:百万以下千以上的数 X thousand X, 10亿以下百万以上的数:X million X thousand X, 10 亿以上的数:X billion X million X thousand X. 每个X分别代表三位数或两位数或一位数。
4.在英式英语中百位数和十位数之间要加and,美式英语中则会省略,我们这个题目采用加上and,百分位为零的话,这道题目我们省略and
下面再看几个数字例句:
1,234: one thousand two hundred and thirty four
8,088: eight thousand (and) eighty eight (注:这个and可加可不加,这个题目我们选择不加)
486,669: four hundred and eighty six thousand six hundred and sixty nine
1,652,510: one million six hundred and fifty two thousand five hundred and ten
说明:
数字为正整数,不考虑小数,转化结果为英文小写;
保证输入的数据合法
具体规则如下:
1.在英语读法中三位数字看成一整体,后面再加一个计数单位。从最右边往左数,三位一单位,例如12,345 等
2.每三位数后记得带上计数单位 分别是thousand, million, billion.
3.公式:百万以下千以上的数 X thousand X, 10亿以下百万以上的数:X million X thousand X, 10 亿以上的数:X billion X million X thousand X. 每个X分别代表三位数或两位数或一位数。
4.在英式英语中百位数和十位数之间要加and,美式英语中则会省略,我们这个题目采用加上and,百分位为零的话,这道题目我们省略and
下面再看几个数字例句:
22: twenty two
100: one hundred
145: one hundred and forty five1,234: one thousand two hundred and thirty four
8,088: eight thousand (and) eighty eight (注:这个and可加可不加,这个题目我们选择不加)
486,669: four hundred and eighty six thousand six hundred and sixty nine
1,652,510: one million six hundred and fifty two thousand five hundred and ten
说明:
数字为正整数,不考虑小数,转化结果为英文小写;
保证输入的数据合法
关键字提示:and,billion,million,thousand,hundred。
数据范围:1 \le n \le 2000000 \
输入描述:
输入一个long型整数
输出描述:
输出相应的英文写法
示例1
输入:
22
输出:
twenty two
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { string line; while ((line = System.Console.ReadLine()) != null) { // 注意 while 处理多个 case string[] groupNames = new string[] { "", "thousand", "million", "billion" }; var dic = GetDic(); string res = ReadNumber(line,dic,groupNames); Console.WriteLine(res); continue; } } static string ReadNumber(string number, Dictionary<string, string> dic, string[] groupNames) { string res = string.Empty; List<string> groups = new List<string>(); for (int i = number.Length - 3; i > -3; i -= 3) { if (i < 0) { groups.Add(number.Substring(0, 3 + i)); } else { groups.Add(number.Substring(i, 3)); } } string group = string.Empty; for (int i = 0; i < groups.Count; i++) { group = string.Empty; if (groups[i].Length==3) { group = ReadThree(groups[i][0].ToString(), groups[i][1].ToString(), groups[i][2].ToString(), dic); } else if (groups[i].Length == 2) { group = ReadTwo(groups[i][0].ToString(), groups[i][1].ToString(), dic); } else if (groups[i].Length == 1) { group = ReadOne(groups[i], dic); } if (group != string.Empty ) { if (i >= 1) { group += " " + groupNames[i]; if (res != string.Empty) { group += " "; } } res = res.Insert(0, group); } } return res; } static string ReadThree(string n1, string n2, string n3, Dictionary<string, string> dic) { string res = string.Empty; string two = ReadTwo(n2, n3, dic); if (n1 != "0") { res += dic[n1]; res += " hundred"; if (two != string.Empty) { res += " and " + two; } } else { res += two; } return res; } static string ReadTwo(string n1, string n2, Dictionary<string, string> dic) { string res = string.Empty; if (n1 == "0") { res += dic[n2]; } else if (n1 == "1") { res += dic[n1 + n2]; } else { res += dic[n1 + "0"]; if (n2 != "0") { res += " " + dic[n2]; } } return res; } static string ReadOne(string n, Dictionary<string, string> dic) { return dic[n]; } static Dictionary<string, string> GetDic() { var dic = new Dictionary<string, string>(); dic.Add("0", ""); dic.Add("1", "one"); dic.Add("2", "two"); dic.Add("3", "three"); dic.Add("4", "four"); dic.Add("5", "five"); dic.Add("6", "six"); dic.Add("7", "seven"); dic.Add("8", "eight"); dic.Add("9", "nine"); dic.Add("10", "ten"); dic.Add("11", "eleven"); dic.Add("12", "twelve"); dic.Add("13", "thirteen"); dic.Add("14", "foutteen"); dic.Add("15", "fifteen"); dic.Add("16", "sixteen"); dic.Add("17", "seventeen"); dic.Add("18", "eighteen"); dic.Add("19", "nineteen"); dic.Add("20", "twenty"); dic.Add("30", "thirty"); dic.Add("40", "forty"); dic.Add("50", "fifty"); dic.Add("60", "sixty"); dic.Add("70", "seventy"); dic.Add("80", "eighty"); dic.Add("90", "ninety"); return dic; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律