If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
うわー面倒くせえw
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProjectEular
{
class Program
{
static void Main(string[] args)
{
//17
int ans = 0;
for (int i = 1; i <= 1000; ++i)
{
ans += problems.problem17(i).Length;
}
Console.WriteLine(ans);
Console.ReadLine();
}
}
class problems
{
public static string problem17(int n)
{
/*
1 one 22 twenty-two
2 two 23 twenty-three
3 three 24 twenty-four
4 four 25 twenty-five
5 five 26 twenty-six
6 six 27 twenty-seven
7 seven 28 twenty-eight
8 eight 29 twenty-nine
9 nine 30 thirty
10 ten 40 forty
11 eleven 50 fifty
12 twelve 60 sixty
13 thirteen 70 seventy
14 fourteen 80 eighty
15 fifteen 90 ninety
16 sixteen 100 a (one) hundred
17 seventeen 101 a (one) hundred (and) one
18 eighteen 102 a (one) hundred (and) two
19 nineteen 111 a (one) hundred (and) eleven
20 twenty 321 three hundred (and) twenty-one
*/
string result = "";
if (n == 1000)
{
result = "onethousand";
return result;
}
if (n >= 100)
{
switch((int)(n/100))
{
case 1:
result = "one";
break;
case 2:
result = "two";
break;
case 3:
result = "three";
break;
case 4:
result = "four";
break;
case 5:
result = "five";
break;
case 6:
result = "six";
break;
case 7:
result = "seven";
break;
case 8:
result = "eight";
break;
case 9:
result = "nine";
break;
default:
break;
}
result += "hundred";
if ((int)(n / 100) * 100 == n)
{
return result;
}
else
{
result += "and";
n -= (int)(n / 100) * 100;
}
}
if (n != 0)
{
if (n < 20)
{
switch (n)
{
case 1:
result += "one";
break;
case 2:
result += "two";
break;
case 3:
result += "three";
break;
case 4:
result += "four";
break;
case 5:
result += "five";
break;
case 6:
result += "six";
break;
case 7:
result += "seven";
break;
case 8:
result += "eight";
break;
case 9:
result += "nine";
break;
case 10:
result += "ten";
break;
case 11:
result += "eleven";
break;
case 12:
result += "twelve";
break;
case 13:
result += "thirteen";
break;
case 14:
result += "fourteen";
break;
case 15:
result += "fifteen";
break;
case 16:
result += "sixteen";
break;
case 17:
result += "seventeen";
break;
case 18:
result += "eighteen";
break;
case 19:
result += "nineteen";
break;
default:
break;
}
}
else
{
switch ((int)(n/10))
{
case 2:
result += "twenty";
break;
case 3:
result += "thirty";
break;
case 4:
result += "forty";
break;
case 5:
result += "fifty";
break;
case 6:
result += "sixty";
break;
case 7:
result += "seventy";
break;
case 8:
result += "eighty";
break;
case 9:
result += "ninety";
break;
default:
break;
}
n -= (int)(n / 10) * 10;
switch (n)
{
case 1:
result += "one";
break;
case 2:
result += "two";
break;
case 3:
result += "three";
break;
case 4:
result += "four";
break;
case 5:
result += "five";
break;
case 6:
result += "six";
break;
case 7:
result += "seven";
break;
case 8:
result += "eight";
break;
case 9:
result += "nine";
break;
default:
break;
}
}
}
return result;
}
}
}
0 件のコメント:
コメントを投稿