from time import time
from random import choice
F = time()
gewei = {1:'one',2:'two',3:'three',4:'four',\
5:'five',6:'six',7:'seven',8:'eight',9:'nine'}
shiji = {11:'eleven',12:'twelve',13:'thirteen',14:'fourteen',15:'fifteen',\
16:'sixteen',17:'seventeen',18:'eighteen',19:'nineteen'}
jishi = {10:'ten',20:'twenty',30:'thirty',40:'forty',50:'fifty',\
60:'sixty',70:'seventy',80:'eighty',90:'ninety'}
jibai = {100:'onehundred',200:'twohundred',300:'threehundred',400:'fourhundred',\
500:'fivehundred',600:'sixhundred',700:'sevenhundred',800:'eighthundred',\
900:'ninehundred'}
yiqian = 'onethousand'
length = 0
a = 0
for i in range(1,1001):
if len(str(i)) == 1:
length += len(gewei[i])
if len(str(i)) == 2:
if i%10 == 0:
length += len(jishi[i])
else:
if 11<=i<=19:
length = length + len(shiji[i])
else:
ge = i%10;shi = (i/10)*10
length = length + len(gewei[ge]) + len(jishi[shi])
if len(str(i)) == 3:
if i%100 == 0:#例如300
length += len(jibai[i])
if (i%100 != 0 and i%10 == 0):#例如320
shi = i - (i/100)*100
bai = i/100
length = length + len(jibai[100*bai]) + len(jishi[shi]) + 3
if (i%100 != 0 and i%10 != 0 and i%100/10 != 0):#例如213,567
ge = i%10
shi = i%100/10
bai = i/100
if 11<=10*shi+ge <=19:
length = length + len(jibai[bai*100])+len(shiji[10*shi+ge]) + 3
else:
length = length + len(jibai[bai*100])+len(jishi[10*shi]) + len(gewei[ge]) + 3
if (i%100 != 0 and i%10 != 0 and i%100/10 == 0):#例如102
bai = i/100;ge = i%10
length = length + len(jibai[100*bai]) + len(gewei[ge]) + 3
if len(str(i)) == 4:
length += len(yiqian)
print length
E = time()
print 'time:'
print (E-F)
|