どーも、みつおです。
英単語の定義がめんどくさい問題だった。
問題
1 から 5 までの数字を英単語で書けば one, two, three, four, five であり, 全部で 3 + 3 + 5 + 4 + 4 = 19 の文字が使われている.
では 1 から 1000 (one thousand) までの数字をすべて英単語で書けば, 全部で何文字になるか.
注: 空白文字やハイフンを数えないこと. 例えば, 342 (three hundred and forty-two) は 23 文字, 115 (one hundred and fifteen) は20文字と数える. なお, “and” を使用するのは英国の慣習.
出典:Problem17
解答
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Problem17 { class Program { static void Main(string[] args) { Console.WriteLine(Solve()); Console.ReadLine(); } private static int Solve() { int ret = 0; List<Number> numlist = new List<Number>(); //1000までの数値と英単語を取得 for (int i = 1; i <= 1000; i++) { Number num = new Number(i); numlist.Add(num); } //英単語の文字を足し合わせる foreach (var tmp in numlist) { ret += tmp.English.Length; } return ret; } } public class Number { #region プロパティ public int No { set; get; } public string English { set; get; } #endregion #region コンストラクタ public Number() { No = 0; English = ""; } public Number(int no) { this.No = no; this.English = GetEnglish(); } #endregion #region メソッド /// <summary> /// 数字を英単語に変換 /// </summary> /// <returns></returns> public string GetEnglish() { string ret = ""; int point = 0; string[] one = new string[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; string[] ten = new string[] { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; string[] hyaku = new string[] { "hundred", "thousand" }; string and = "and"; point = this.No; while (point != 0) { if (1 <= point && point <= 19) { ret += one[point - 1]; point = 0; } else if (20 <= point && point <= 29) { ret += ten[0]; point = point - 20; } else if (30 <= point && point <= 39) { ret += ten[1]; point = point - 30; } else if (40 <= point && point <= 49) { ret += ten[2]; point = point - 40; } else if (50 <= point && point <= 59) { ret += ten[3]; point = point - 50; } else if (60 <= point && point <= 69) { ret += ten[4]; point = point - 60; } else if (70 <= point && point <= 79) { ret += ten[5]; point = point - 70; } else if (80 <= point && point <= 89) { ret += ten[6]; point = point - 80; } else if (90 <= point && point <= 99) { ret += ten[7]; point = point - 90; } else if (100 <= point && point <= 199) { ret += one[0]; ret += hyaku[0]; point = point - 100; if (point != 0) ret += and; } else if (200 <= point && point <= 299) { ret += one[1]; ret += hyaku[0]; point = point - 200; if (point != 0) ret += and; } else if (300 <= point && point <= 399) { ret += one[2]; ret += hyaku[0]; point = point - 300; if (point != 0) ret += and; } else if (400 <= point && point <= 499) { ret += one[3]; ret += hyaku[0]; point = point - 400; if (point != 0) ret += and; } else if (500 <= point && point <= 599) { ret += one[4]; ret += hyaku[0]; point = point - 500; if (point != 0) ret += and; } else if (600 <= point && point <= 699) { ret += one[5]; ret += hyaku[0]; point = point - 600; if (point != 0) ret += and; } else if (700 <= point && point <= 799) { ret += one[6]; ret += hyaku[0]; point = point - 700; if (point != 0) ret += and; } else if (800 <= point && point <= 899) { ret += one[7]; ret += hyaku[0]; point = point - 800; if (point != 0) ret += and; } else if (900 <= point && point <= 999) { ret += one[8]; ret += hyaku[0]; point = point - 900; if (point != 0) ret += and; } else if (1000 <= point && point <= 1999) { ret += one[0]; ret += hyaku[1]; point = point - 1000; if (point != 0) ret += and; } } return ret; } #endregion } }
出力
21124