数字的英文表达(趣味题C#)

趣味题大笑

输入一个正整数N(N最大是4位数),输出它的英文表达

    class Program
    {
        static void Main(string[] args)
        {
            var table = aa();
            print(table, 1024);
            Console.WriteLine();
            print(table, 135);
            Console.WriteLine();
            print(table, 29);
            Console.WriteLine();
            print(table, 10);
            Console.WriteLine();
            print(table, 8);
        }

        static Dictionary<int, string> aa()
        {
            Dictionary<int, string> table = new Dictionary<int, string>(29);
            table.Add(0, "zero");
            table.Add(1, "one");
            table.Add(2, "two");
            table.Add(3, "three");
            table.Add(4, "four");
            table.Add(5, "five");
            table.Add(6, "six");
            table.Add(7, "seven");
            table.Add(8, "eight");
            table.Add(9, "nine");
            table.Add(10, "ten");
            table.Add(11, "eleven");
            table.Add(12, "twelve");
            table.Add(13, "thirteen");
            table.Add(14, "fourteen");
            table.Add(15, "fifteen");
            table.Add(16, "sixteen");
            table.Add(17, "seventeen");
            table.Add(18, "eighteen");
            table.Add(19, "ninteen");
            table.Add(20, "twenty");
            table.Add(30, "thirty");
            table.Add(40, "forty");
            table.Add(50, "fifty");
            table.Add(60, "sixty");
            table.Add(70, "seventy");
            table.Add(80, "eighty");
            table.Add(90, "ninty");
            table.Add(100, "hundred");
            table.Add(1000, "thousand");

            return table;
        }

        //输入一个正整数N(N最大是4位数),输出它的英文表达
        static void print(Dictionary<int, string> table, int n)
        {
            if (n >= 0 && n <= 19)
            {
                Console.Write(table[n]);         //直接输出19以内的数字  
            }
            else if (n >= 20 && n <= 99 && n % 10 == 0)     //整十  
            {
                Console.Write(table[n / 10 * 10]);
            }
            else if (n >= 20 && n <= 99)          //先输出十位,再输出个位  
            {
                Console.Write(table[n / 10 * 10] + " " + table[n % 10]);
            }
            else if (n >= 100 && n <= 999)
            {
                print(table, n / 100);
                Console.Write(" " + table[100] + " ");    //输出百位  
                print(table, n % 100);                //递归调用,输出十位和个位  
            }
            else if (n >= 1000 && n <= 999999)
            {
                print(table, n / 1000);
                Console.Write(" " + table[1000] + " ");    //输出千位  
                print(table, n % 1000);               //递归调用,输出百位、十位和个位  
            }
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值