Re: [閒聊] 每日leetcode

看板Marginalman作者 (通通打死)時間1年前 (2024/08/07 22:02), 編輯推噓0(002)
留言2則, 2人參與, 1年前最新討論串662/1548 (看更多)
點點點 其實還蠻好玩的題目== 最後WA了一個1000000 不小心輸出one million thousand 姆咪 def numberToWords(self, num: int) -> str: a = ["","One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine ","Ten ", "Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen ","Twenty "] b = ["","","Twenty ","Thirty ","Forty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety "] c = ["","Thousand ","Million ","Billion ","Trillion "] def smaller_than_thousand(n): if n >= 1000: print("gg") return "" # for < 1000 d = [100,10] ans = "" idx = 0 while n>0: num_cur = n % d[idx] n = n//d[idx] if idx == 1: ans = a[num_cur] + "Hundred " + ans idx += 1 continue if num_cur <= 20: ans = a[num_cur] + ans idx += 1 continue else: tmp = "" tmp += b[(num_cur//10)] num_cur = num_cur%10 tmp += a[num_cur] ans = tmp + ans idx += 1 continue return ans ans = "" idx = 0 while num>0: num_cur = num%1000 if num_cur != 0: ans = smaller_than_thousand(num_cur) + c[idx] + ans num = num//1000 idx += 1 return "Zero" if ans == "" else ans[:-1] -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1723039329.A.8A9.html

08/07 22:02, 1年前 , 1F
大師
08/07 22:02, 1F

08/08 01:16, 1年前 , 2F
誰可以提交第一次就AC就可以說這題簡單
08/08 01:16, 2F
文章代碼(AID): #1citvXYf (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1citvXYf (Marginalman)