python算法题--简单-- K 进制表示下的各位数字总和

class Solution:
    def sumBase(selfnintkint) -> int:
        tag = True
        quotient = n//k  #商
        remainder = n%k #余数
        while tag:
            if quotient >= k:
                remainder+=quotient%k #若商比k大,则可以继续除下去,将余数加至remainder
                quotient//=k
            else:
                remainder+=quotient #若商小于k,则剩余的商即为余数,加至remainder
                tag=False
        return remainder

 

posted @ 2022-09-09 16:06  盗哥泡茶去了  阅读(25)  评论(0编辑  收藏  举报