张德长

导航

牛客[编程题] HJ42 学英语

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

下面再看几个数字例句:
22: twenty two
100:  one hundred
145:  one hundred and forty five
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

说明:
数字为正整数,不考虑小数,转化结果为英文小写;
保证输入的数据合法
关键字提示: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;
        }
    }

 

posted on 2023-11-09 16:09  张德长  阅读(22)  评论(0编辑  收藏  举报