g138. 英文數字拼寫字母數

中等查表回報錯誤

時間限制 1000 ms ・ 記憶體限制 256 MB

題目描述

把正整數用英文拼寫出來,並統計其中英文字母的個數(空白與連字號 - 都不計入)。拼寫規則如下:

  • 11 到 99:one two three four five six seven eight nine
  • 1010 到 1919:ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
  • 2020 到 9999:十位用 twenty thirty forty fifty sixty seventy eighty ninety,若個位不為 00,再接上個位的拼寫(中間用連字號,例如 forty-two)
  • 百位:數字 hh(1≤h≤91 \le h \le 9)對應 h hundred,例如 three hundred
  • 千位:數字 tt(1≤t≤91 \le t \le 9)對應 t thousand,例如 five thousand
  • 由高位到低位依序拼寫(千、百、後兩位)。只要整數 n≥100n \ge 100 且 nn 的後兩位數(n mod 100n \bmod 100)不為 00,就要在後兩位數的拼寫前面加上單字 and。

例如 342342 拼成 three hundred and forty-two(5+7+3+5+3=235+7+3+5+3=23 個字母),10051005 拼成 one thousand and five,11001100 拼成 one thousand one hundred,115115 拼成 one hundred and fifteen。

給定 QQ 筆查詢,每筆查詢給整數 l,rl, r,請計算 ll 到 rr(含)的所有整數的拼寫字母數總和。

輸入格式

第一行一個整數 QQ。 接下來 QQ 行,每行兩個整數 ll、rr。

  • 1≤Q≤20001 \le Q \le 2000
  • 1≤l≤r≤99991 \le l \le r \le 9999

輸出格式

輸出 QQ 行,每行一個整數,表示對應查詢的字母數總和。

範例

見下方範例輸入與輸出。

範例一:11 到 55 為 one two three four five,共 3+3+5+4+4=193+3+5+4+4=19 個字母;342342 為 2323 個字母;100100 為 one hundred(1010 個字母),101101 為 one hundred and one(1616 個字母),兩者總和為 2626。

範例測資

範例輸入 1

3
1 5
342 342
100 101

範例輸出 1

19
23
26

範例輸入 2

2
9999 9999
1 10

範例輸出 2

36
39
請先登入後再提交程式碼

討論與題解

載入討論區…

更多