趣味题
输入一个正整数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); //递归调用,输出百位、十位和个位
}
}
}