如果把1到5写成英文单词,分别是:one, two, three, four, five,这些单词一共用了3 + 3 + 5 + 4 + 4 = 19个字母。
求 如果把1到1000都写成英文单词,一共要用多少个字母?
- 注意: 不要算上空格和连字符。例如,342(three hundred and forty-two)包含23个字母,
- 115(one hundred and fifteen)包含20个字母。单词“and”的使用方式遵循英式英语的规则。
output: 21124
方法一
public static int NumberLetterCounts(){
int sum = 0; //字母的和
for(int i = 1; i<21; i++) {
sum = sum + LetterCountsOne(i);
}
for(int i= 21; i<100; i++) {
sum = sum + LetterCountsTwo(i/10) + LetterCountsOne(i%10);
}
for(int i = 100; i<1000; i++) {
//[100, 119] [200, 219] [100, 119]...[921, 999]
if(i%100 <= 20)
//如果是整百的数 one/two hundred --7; 如果不是整百 hundred and --10
sum = sum + LetterCountsOne(i/100) + (i % 100 == 0 ? 7:10) + LetterCountsOne(i%100);
//[121, 199] [221, 299] [321, 399]...[921, 999]
else
sum = sum + LetterCountsOne(i/100) + (i % 100 == 0 ? 7:10)
+ LetterCountsTwo(i%100/10) + LetterCountsOne(i%10);
}
// one thousand 1000
sum = sum + 11;
return sum;
}
public static int LetterCountsOne(int num) {
if(num == 1) return 3; //one
if(num == 2) return 3; //two
if(num == 3) return 5; //three
if(num == 4) return 4; //four
if(num == 5) return 4; //five
if(num == 6) return 3; //six
if(num == 7) return 5; //seven
if(num == 8) return 5; //eight
if(num == 9) return 4; //nine
if(num == 10) return 3; //ten
if(num == 11) return 6; //eleven
if(num == 12) return 6; //twelve
if(num == 13) return 8; //thirteen
if(num == 14) return 8; //fourteen
if(num == 15) return 7; //fifteen
if(num == 16) return 7; //sixteen
if(num == 17) return 9; //seventeen
if(num == 18) return 8; //eighteen
if(num == 19) return 8; //nineteen
if(num == 20) return 6; //twenty
return 0;
}
public static void main(String[] args) {
System.out.println(NumberLetterCounts());
}
方法二
//方法2
public static int NumberLetterCounts2(){
// 1-19
//因为数组下标从0开始,而个位数字对应数组下标,所以0要用空字符串“”表示
String[] num1 = new String[] {"","one","two","three","four","five","six","seven","eight",
"nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen",
"seventeen","eighteen","nineteen"};
String[] num2 = new String[] {"","","twenty","thirty","forty","fifty","sixty",
"seventy","eighty","ninety"};
int sum = 0; //字母的和
for(int i=1; i<20; i++) {
sum = sum + num1[i].length();
}
for(int i=20; i<100; i++) {
int a = i % 10; //个位
int b = i / 10; //十位
sum = sum + num1[a].length() + num2[b].length();
}
for(int i=100; i<1000; i++) {
//[100, 119] [200, 219] [100, 119]...[921, 999]
if(i % 100 < 20) {
int c = i / 100; //百位
int a = i % 100; // [0, 19]
//或 int a = (i - 100*c); //[0, 19]
//如果是整百的数 one/two hundred --7; 如果不是整百 hundred and --10
sum = sum + num1[c].length() + (i % 100 == 0 ? 7 : 10) + num1[a].length();
}
//[121, 199] [221, 299] [321, 399]...[921, 999]
else {
int c = i / 100; //百位
int a = i % 10; //个位
int b = (i % 100) / 10; //十位
// 或 int b = ((i-a)/10) % 10;
sum = sum + num1[c].length() + (i % 100 == 0 ? 7 : 10) + num1[a].length() + num2[b].length();
}
}
// one thousand 1000
sum = sum + 11;
return sum;
}
public static void main(String[] args) {
System.out.println(NumberLetterCounts2());
}
方法三
public class Test {
public static void main(String[] args) {
String[] m=new String[]{"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
String[] n=new String[]{"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
int first,second,three,sum=0;
for (int i=1;i<1000;++i)
{
int a=i%10;//个位数字
int b=(i%100)/10;//十位
int c=i/100;//百位
if(c >= 1)
{
three = m[c].length() + 7;
if(i % 100 != 0)
three += 3;
}
else
three = 0;
if(b >= 2)
{
second = n[b].length();
first = m[a].length();
}
else
{
second = 0;
first = m[i % 100].length();
}
sum = sum + first + second + three;
}
sum+=11;
System.out.println(sum);
}
}
参考文献https://blog.csdn.net/devmode/article/details/8687879
- 如果是整百的数 one/two hundred --7; 如果不是整百 hundred and --10
(i % 100 == 0 ? 7:10) - 表示数组中某个元素的长度 – num1[i].length()。