どーも、みつおです。
英単語の定義がめんどくさい問題だった。
問題
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
解答
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
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
コメントを残す