C# 我的程序返回21144而不是21124,为什么?
数字字母计数 问题17 如果数字1到5是用文字写出来的:一、二、三、四、五,那么总共使用了3+3+5+4+4=19个字母 如果所有从1到1000(一千)的数字都是用文字写出来的,那么会使用多少个字母 注意:不要计算空格或连字符。例如,342(三百四十二)包含23个字母,115(一百一十五)包含20个字母。写数字时使用“and”符合英国用法 尝试将一些数字与所有匹配的字符串进行比较C# 我的程序返回21144而不是21124,为什么?,c#,C#,数字字母计数 问题17 如果数字1到5是用文字写出来的:一、二、三、四、五,那么总共使用了3+3+5+4+4=19个字母 如果所有从1到1000(一千)的数字都是用文字写出来的,那么会使用多少个字母 注意:不要计算空格或连字符。例如,342(三百四十二)包含23个字母,115(一百一十五)包含20个字母。写数字时使用“and”符合英国用法 尝试将一些数字与所有匹配的字符串进行比较 using System; using System.Collections.Generic; public cl
using System;
using System.Collections.Generic;
public class Program
{
static int n = 1000;
static List<int> letterCountCache = new List<int>();
public static void Main()
{
long t = 0;
letterCountCache.Add(0);
for (int i = 1; i <= n; i ++)
{
letterCountCache.Add(GetLetterCount(i));
t += letterCountCache[i];
}
Console.WriteLine(t);
Console.WriteLine(letterCountCache[70] + " " + "seventy".Length);
}
static int GetLetterCount(int i)
{
if (letterCountCache.Count > i)
return letterCountCache[i];
switch (i)
{
case 1:
return "one".Length;
case 2:
return "two".Length;
case 3:
return "three".Length;
case 4:
return "four".Length;
case 5:
return "five".Length;
case 6:
return "six".Length;
case 7:
return "seven".Length;
case 8:
return "eight".Length;
case 9:
return "nine".Length;
case 10:
return "ten".Length;
case 11:
return "eleven".Length;
case 12:
return "twelve".Length;
case 13:
return "thirteen".Length;
case 14:
return "fourteen".Length;
case 15:
return "fifteen".Length;
case 16:
return "sixteen".Length;
case 17:
return "seventeen".Length;
case 18:
return "eighteen".Length;
case 19:
return "nineteen".Length;
}
if (i >=20 && i <=29)
return "twenty".Length + GetLetterCount(i % 10);
if (i >=30 && i <=39)
return "thirty".Length + GetLetterCount(i % 10);
if (i >= 40 && i <= 49)
return "forty".Length + GetLetterCount(i % 10);
if (i >=50 && i <= 59)
return "fifty".Length + GetLetterCount(i % 10);
if (i > 80 && i < 89)
return "eighty".Length + GetLetterCount(i % 10);
if (i >= 60 && i <= 99)
return GetLetterCount(i % 10) + "ty".Length + GetLetterCount(i / 10);
if (i == 1000)
return "onethousand".Length;
if (i % 100 == 0)
return GetLetterCount(i / 100) + "hundred".Length;
return GetLetterCount(i / 100) + "hundred".Length + "and".Length + GetLetterCount(i % 100);
}
}
使用系统;
使用System.Collections.Generic;
公共课程
{
静态整数n=1000;
静态列表letterCountCache=新列表();
公共静态void Main()
{
长t=0;
letterCountCache.Add(0);
对于(int i=1;i i)
返回letterCountCache[i];
开关(一)
{
案例1:
返回“一”。长度;
案例2:
返回“2”。长度;
案例3:
返回“三”。长度;
案例4:
返回“四”。长度;
案例5:
返回“五”。长度;
案例6:
返回“六”。长度;
案例7:
返回“七”。长度;
案例8:
返回“八”。长度;
案例9:
返回“九”。长度;
案例10:
返回“十”。长度;
案例11:
返回“十一”。长度;
案例12:
返回“十二”。长度;
案例13:
返回“十三”。长度;
案例14:
返回“十四”。长度;
案例15:
返回“十五”。长度;
案例16:
返回“十六”。长度;
案例17:
返回“十七”。长度;
案例18:
返回“十八”。长度;
案例19:
返回“十九”。长度;
}
如果(i>=20&&i=30&&i=40&&i=50&&i=80&&i<89)
返回“八十”。长度+GetLetterCount(i%10);
如果(i>=60&&i条件中缺少等号:
if (i >= 80 && i <= 89)
return "eighty".Length + GetLetterCount(i % 10);
如果(i>=80&&i我猜这是循环中的停止条件。“一个小时”和不应该是两个词吗?“一千”
@DmitryBychenko他删除了空格,因此他不必计算空格。就像任何连字符一样。你没有返回字符串。empty.length0@DanielBrughera发现了这一点,但它从未发生过,因为它被顶部的“缓存”捕获,在方法运行之前,为索引0添加了一个零项。如果没有此项,您将获得eightty
和eighttynine
。它错误地计算了每100个数字中的2个额外字母。因此,给定1000个数字,它可以解释+20之前的答案是错误的。