0
0

More than 1 year has passed since last update.

ABC192 C - Kaprekar Number を解いた

Last updated at Posted at 2021-10-18

abc192_1.png
abc192_2.png
abc192_3.png

文章のままを書けばいいと思う。

abc192c.py
N,K = map(int,input().split())

for _ in range(K):#O(10^5)
    num0 = sorted(list(str(N)))
    num1 = sorted(num0,reverse=True)
    g1 = int("".join(num1))
    g2 = int("".join(num0))
    N = g1-g2
print(N)#192ms

スピードをあげるために def は must.
def の中に def を埋め込んでみた。

abc192c.py
def solv():
    N,K = map(int,input().split())

    def helper(n):
        num = "".join(sorted(list(str(n))))
        return int(num[::-1])-int(num)

    for _ in range(K):
        N = helper(N)
    print(N)

solv()#132ms

60ms 改善できた。30ms 代の回答を読んだが
全く訳が分からん。また時間を置いてチャレンジしよう。

0
0
0

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