zoukankan      html  css  js  c++  java
  • Euler Project question 17 in python way

    # This Python file uses the following encoding: utf-8
    # 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.
    import time
    start = time.time()
    d = {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:8, 19:8, 20:6, 30:6, 40:5, 50:5, 60:5, 70:7, 80:6, 90:6, 100:7, 1000:8, 'and':3}
    letters = 0
    for i in xrange(1, 1001):
        if i <= 20:
            letters += d[i]
        if 20 < i < 100:
            letters += d[i - i%10] + d[i%10]
        if 100 <= i < 1000:
            letters += d[i/100] + d[100]
            if i%100 != 0:
                letters += d['and']
                if i%100 <= 20:
                    letters += d[i%100]
                else:
                    letters += d[i%100 - i%100%10] + d[i%10]
        if i == 1000:
            letters += d[1]
            letters += d[1000]

    print "%s letters in %s seconds" % (letters, time.time() - start)

  • 相关阅读:
    Eclipse快捷键
    LeeCode
    Code Complete
    Git
    sql优化策略
    FSA/FSM/FST
    索引失效情况
    实现HttpHandlerFactory的方法
    Xpath语法格式整理
    Edojs应用
  • 原文地址:https://www.cnblogs.com/cyberpaz/p/4060584.html
Copyright © 2011-2022 走看看