0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【初心者】pythonで0から Project Euler を解いてみた17

Posted at

今回はeuler17問目を解いていきます!
問題はここです!!
http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2017
では、解いていきます!!
##私の考え
まず問題を読んだときに数と英語をどうやって関連付けるか考え調べましたそしたら辞書(dict)が出てきました
それで一からすべてに割り当てるのは大変だから法則を見つけそれでと思いましたコードを見ればわかると思います。
#コード


English_number_li = []
def English_number(n):
    eng_num = {
    0:"",
    1:"one",
    2:"two",
    3:"three",
    4:"four",
    5:"five",
    6:"six",
    7:"seven",
    8:"eight",
    9:"nine",
    }
    eng_ten = {
    0:"ten",
    1:"eleven",
    2:"twelve",
    3:"thirteen",
    4:"fourteen",
    5:"fifteen",
    6:"sixteen",
    7:"seventeen",
    8:"eighteen",
    9:"nineteen",
    }
    eng_teen = {
    2:"twenty",
    3:"thirty",
    4:"forty",
    5:"fifty",
    6:"sixty",
    7:"seventy",
    8:"eighty",
    9:"ninety",
    }
    eng = {
    100:"hundred",
    1000:"onethousand",
    }
    num = n
    Num = list(str(num))
    #print(Num)
    #rint(Num[1])
    if num == 1000:
        txt = (eng[1000])
    if 100 <= num < 1000:
        if int(Num[1]) == 1:
            txt = (eng_num[int(Num[0])] + eng[100] + "and" + eng_ten[int(Num[2])])
        if int(Num[1]) == 0:
            if int(Num[1]) == 0 and int(Num[2]) == 0:
                txt = (eng_num[int(Num[0])] + eng[100])
            else:    
                txt = (eng_num[int(Num[0])] + eng[100] + "and" + eng_num[int(Num[2])])
        if int(Num[1]) > 1:
            txt = (eng_num[int(Num[0])] + eng[100] + "and" + eng_teen[int(Num[1])] + eng_num[int(Num[2])])
    if 20 <= num < 100:
        txt = (eng_teen[int(Num[0])] + eng_num[int(Num[1])])
    if 10 <= num < 20:
        txt = (eng_ten[int(Num[1])])
    if 0 < num < 10:
        txt = (eng_num[int(Num[0])])
    English_number_li.append(txt)

total = 0

for i in range(1000):
    i += 1
    English_number(i)
    total += int(len(English_number_li[i-1]))

print(English_number_li)
print(len(English_number_li[5]))
print(total)
#答えは21124

まずある程度を定義しておきます
定義するのは1~19と10の倍数と100と10000を定義します
そしたらfor文で一文字ずつ足してゴリゴリ計算していきます!!
そして文字の数(len)をたして完成(⋈◍>◡<◍)。✧♡

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?