解答不计算17的答案中的字母数

时间:2014-04-28 11:37:56

标签: dart

我制作了一个解决问题17的程序

Number letter counts
Problem 17
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.

我的尝试,在飞镖中,减去主要方法,如下所示

isSpecialBelow100(number) {
  switch(number) {
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
      case 8:
      case 9:
      case 10:
      case 11:
      case 12:
      case 13:
      case 14:
      case 15:
      case 16:
      case 17:
      case 18:
      case 19:
      case 20:
      case 30:
      case 40:
      case 50:
      case 60:
      case 70:
      case 80:
      case 90:
        return true;
      default:
        return false;
  }
}


removeExcess(number, weight) => number - number % weight;

getExcess(number, weight) => number % weight;

specialNumberToWord(number) {
  switch(number) {
    case 1:
      return 'one';
    case 2:
      return 'two';
    case 3:
      return 'three';
    case 4:
      return 'four';
    case 5:
      return 'five';
    case 6:
      return 'six';
    case 7:
      return 'seven';
    case 8:
      return 'eight';
    case 9:
      return 'nine';
    case 10:
      return 'ten';
    case 11:
      return 'eleven';
    case 12:
      return 'twelve';
    case 13:
      return 'thirteen';
    case 14:
      return 'forteen';
    case 15:
      return 'fifteen';
    case 16:
      return 'sixteen';
    case 17:
      return 'seventeen';
    case 18:
      return 'eighteen';
    case 19:
      return 'nineteen';
    case 20:
      return 'twenty';
    case 30:
      return 'thirty';
    case 40:
      return 'forty';
    case 50:
      return 'fifty';
    case 60:
      return 'sixty';
    case 70:
      return 'seventy';
    case 80:
      return 'eighty';
    case 90:
      return 'ninety';
    default:
      return '';
  }
}


printProblem17Result() {
  var words = new StringBuffer();
  for(var number = 1; number <= 1000; number++) {
    if(number < 100) {
      if(isSpecialBelow100(number)) {
        words.write(specialNumberToWord(number));
      } else {
        words.write(specialNumberToWord(removeExcess(number, 10)));
        words.write(' ');
        words.write(specialNumberToWord(getExcess(number, 10)));
      }
    } else if(number >= 100 && number < 1000) {
      var remaining = int.parse(removeExcess(number, 100).toString().replaceAll('0', ''));
      words.write(specialNumberToWord(remaining));
      words.write(' hundred ');
      var excess = getExcess(number, 100);
      if(excess != 0) {
        words.write(' and ');
        if(isSpecialBelow100(excess)) {
          words.write(specialNumberToWord(excess));
        } else {
          words.write(specialNumberToWord(removeExcess(excess, 10)));
          words.write(' ');
          words.write(specialNumberToWord(getExcess(excess, 10)));
        }
      }
    } else {
      words.write(' one thousand');
    }
    words.write(' ');
  }

  print(words.toString().split(' ').fold(0, (prev, current) => prev + current.length));
}

虽然我认为可能不是最整洁的,但应该产生正确数量的字符。

似乎没有达到10的解决方案。谁能告诉我我做错了什么?

更新:

我没有添加拼写修正,但我确实删除了换行符并用空格替换它,并更正了拆分方法以拆分空格。我最初发布的版本是通过备受诟病的打印行方法尝试调试。

实际上14的正确拼写是十四,而不是十四,这有时很容易被忽视。

1 个答案:

答案 0 :(得分:2)

你有14个拼写错误应该是十四个不是&#39;十四&#39;。

此外,您似乎正在使用

计算新的行字符
  print( (words.toString().replaceAll(' ','').replaceAll('\n','')).length);

纠正这个问题,你应该得到答案21124.