回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>请告诉我如何修复此代码。我试过纠正很多事情,但我有10个额外的解决方案!</p>
<p>如果数字1到5是用单词写出来的:1,2,3,4,5,那么总共有3+3+5+4+4=19个字母。</p>
<p>如果所有从1到1000(一千)的数字都是用文字写出来的,会用多少个字母?</p>
<p>注意:不要计算空格或连字符。例如,342(三百四十二)包含23个字母,115(一百一十五)包含20个字母。写数字时使用“and”符合英国用法。</p>
<p>我的解决方案</p>
<pre><code>sd={0:0,1: 3, 2: 3, 3: 5, 4: 4, 5: 4, 6: 3, 7: 5, 8: 5, 9: 4}
dd1={10:3,11:6,12:6,13:8,14:8,15:7,16:7,17:9,18:9,19:8}
dd2={2:6,3:6,4:5,5:5,6:5,7:7,8:6,9:6}
td= {0: 10, 1: 13, 2: 13, 3: 15, 4: 14, 5: 14, 6: 13, 7: 15, 8: 15, 9: 14}
cd={0:0,1: 3, 2: 3, 3: 5, 4: 4, 5: 4, 6: 3, 7: 5, 8: 5, 9: 4,10:3,11:6,12:6,13:8,14:8,15:7,16:7,17:9,18:9,19:8}
def cw(n) :
if n/10 == 0 : # If the number is less than 10 execute this section
return sd[n%10]
elif n/100 == 0 : # If the number is less than 100 execute this section
if n<20 :
return(dd1[n]) # Directly map to dd1
else :
return(dd2[n/10]+sd[n%10]) # If the number is > 20 do a construction
elif n/1000==0 :
if n%100==0:
return sd[n/100] + 7 # If the number is multiples of 100 give assuming single digit and 7 for hundred
elif n%100 < 20 :
return td[n/100] + cd[n%100] # If 3 digit numbers not more than *20 , then direct mapping
else :
return td[n/100] + dd2[(n%100)/10] + sd[n%10]
count = 0
for i in range(1,1000) :
count = count + cw(i)
print count + 11
</code></pre>
<p>我得到21134,答案是。。。(扰流器:请将鼠标悬停在下一行上查看)</p>
<blockquote class="spoiler">
<p> 21124</p>
</blockquote>
<p>很烦人!</p>